Search code examples
objective-cnsdatansurlcs193p

NSData initWithContentsOfURL Error


i'm trying to load an image from web. For initialize the Data i use this method:

NSData *imageData = [[NSData alloc] initWithContentsOfURL:self.imageUrl];

but i get this error:

[__NSCFString isFileURL]: unrecognized selector sent to instance 0x817a4f0
2013-08-26 09:45:53.221 CorePhoto[1643:3f03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString isFileURL]: unrecognized selector sent to instance 0x817a4f0'

And of course self.imageUrl is an NSURL property.

this is an example of imageUrl url:

http://farm5.static.flickr.com/4016/4555417946_e6332481b3_b.jpg

this is the complete code of my method:

- (void)resetImage
{
    if (self.scrollView) {
        if (self.imageUrl) {
            //before to set the content, we reset it
            self.scrollView.contentSize = CGSizeZero;
            self.imageView.image = nil;
            [self.spinner startAnimating];
            dispatch_queue_t loadImageQ = dispatch_queue_create("load image thread", NULL);
            dispatch_async(loadImageQ, ^(void) {
                [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
                NSData *imageData = [[NSData alloc] initWithContentsOfURL:self.imageUrl];
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                UIImage *image = [[UIImage alloc] initWithData:imageData];
                dispatch_async(dispatch_get_main_queue(), ^(void) {
                    if (image) {
                        self.scrollView.zoomScale = 1.0;
                        self.scrollView.contentSize = image.size;
                        self.imageView.image = image;
                        self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
                    }
                    [self.spinner stopAnimating];
                });
            });
        }
    }
}

what is wrong with my initWithContentsOfURL method? thanks thanks


Solution

  • imageUrl may be defined as an NSURL property but you have set its value to an NSString instance. This may not give you a compiler warning in a number of cases and won't throw an exception until you try to use it.

    Check where you set imageUrl and ensure you verify the type and create an NSURL.