views:

1687

answers:

3

I have a URL for an image (got it from UIImagePickerController) but I no longer have the image in memory (the URL was saved from a previous run of the app). Can I reload the UIImage from the URL again?

I see that UIImage has a imageWithContentsOfFile: but I have a URL. Can I use NSData's dataWithContentsOfURL: to read the URL?

-----EDIT1----- based on @Daniel's answer I tried the following code but it doesn't work...

NSLog(@"%s %@", __PRETTY_FUNCTION__, photoURL);     
if (photoURL) {
    NSURL* aURL = [NSURL URLWithString:photoURL];
    NSData* data = [[NSData alloc] initWithContentsOfURL:aURL];
    self.photoImage = [UIImage imageWithData:data];
    [data release];
}

When I ran it the console shows:

-[PhotoBox willMoveToWindow:] file://localhost/Users/gary/Library/Application%20Support/iPhone%20Simulator/3.2/Media/DCIM/100APPLE/IMG_0004.JPG
*** -[NSURL length]: unrecognized selector sent to instance 0x536fbe0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL length]: unrecognized selector sent to instance 0x536fbe0'

Looking at the call stack, I'm calling URLWithString, which calls URLWithString:relativeToURL:, then initWithString:relativeToURL:, then _CFStringIsLegalURLString, then CFStringGetLength, then forwarding_prep_0, then forwarding, then -[NSObject doesNotRecognizeSelector].

Any ideas why my NSString (photoURL's address is 0x536fbe0) doesn't respond to length? Why does it say it doesn't respond to -[NSURL length]? Doesn't it know that param is an NSString, not a NSURL?

------EDIT2----- OK, the only problem with the code is the string to URL conversion. If I hardcode the string, everything else works fine. So something is wrong with my NSString and if I can't figure it out, I guess that should go in as a different question. With this line inserted (I pasted the path from the console log above), it works fine:

photoURL = @"file://localhost/Users/gary/Library/Application%20Support/iPhone%20Simulator/3.2/Media/DCIM/100APPLE/IMG_0004.JPG";
+4  A: 

You can do it this way (synchronously, but compact):

NSImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSUrl NSURL URLWithString:MyURL]]];

A much better approach is to use Apple's LazyTableImages to preserve interactivity.

Best, -dan

Daniel Blezek
Doesn't the data need to be converted from PNG (or JPG) file format to UIImage data? Or does UIImage figure that out somehow?
progrmr
Guess I should just RTFM, it says right in UIImage Class Reference what file formats it can support and imageWithData: says it can be data from a file, sounds like it should work.
progrmr
@Daniel: didn't work, I edited my question to include my actual code and the exception info. It's a little bizarre.
progrmr
@Daniel: it does work if I use a string constant instead of the NSString I was passing in. So this latest problem was something wrong with my NSString, not a problem with the NSURL/NSData/UIImage code which works. Thanks Daniel!
progrmr
+1  A: 

Check out the AsyncImageView provided over here. Some good example code, and might even be usable right "out of the box" for you.

marcc
Interesting example, it's very similar in concept to Apple's LazyTableImages example mentioned in the earlier answer.
progrmr
A: 

Thanks Daniel it works

Yogesh