0

I can't seem to get an online GIF image to load into an NSImageView. Please see my code below and offer some comments to remedy this.

#import <Foundation/Foundation.h>

@interface ImageFromUrl : NSObject

@property (strong) NSString *urlString;
@property (strong) NSURL *url;
@property (strong) NSImage *radarGif;
@property (weak) IBOutlet NSImageView *radarGifView;

- (void)displayUrlImage:(id)sender;

@end

and

#import "ImageFromUrl.h"

@implementation ImageFromUrl

@synthesize urlString, url, radarGif, radarGifView;

- (void)displayUrlImage:(id)sender {
    urlString = @"http://icons.wunderground.com/data/640x480/2xradarb5.gif";
    url = [NSURL URLWithString:urlString];

    radarGif = [[NSImage alloc] initWithContentsOfURL:url];
    [radarGifView setImage:radarGif];
}

@end

I am receiving no errors when I run the application and the image of the GIF is not showing in the window.

wigging
  • 8,492
  • 12
  • 75
  • 117
  • What result are you seeing? Any error messages? Did you hook up your outlet to your image view? – rdelmar Apr 23 '12 at 23:48
  • Check and make sure the imageView even exists – Otium Apr 24 '12 at 01:01
  • Also, do NOT load images in this way. You should load remote resources asynchronously, your code will block the main thread until the image is received from the remote server. You should use `NSURLConnection` or a third-party alternative. – Rob Keniger Apr 24 '12 at 01:06
  • Nevertheless, it is **NOT** the correct way to load remote images. You will definitely cause problems for users if you load images synchronously in this fashion. – Rob Keniger Apr 24 '12 at 01:32
  • 1
    I put this code in a project, and it worked fine, so the code works. How did you connect the outlet to the image view (do you have an object in IB that's an instance of this class)? How are you calling the method (and from where)? – rdelmar Apr 24 '12 at 02:04

1 Answers1

2

I put this code in a project, and it worked fine, so the code works. How did you connect the outlet to the image view (do you have an object in IB that's an instance of this class)? How are you calling the method (and from where)?

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Duh, I forgot to call the method. I just added 'awakeFromNib' to call the 'displayUrlImage' method and everything worked fine. Thanks for pointing this out. Sometimes I forget the basics, especially if I haven't programmed in a while. – wigging Apr 24 '12 at 13:32