Load web image into iphone native applicaiton UIImage with no NSURLConnection code required
It turns out it is trivial to load images into iPhone native applications.
This simple function will do all of the work. No UIWebView required no NSURLConnection required. Just dataWithContentsOfURL.
-(UIImage*) newUIImageWithURLString:(NSString*)urlString
{
return [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
}
Usage:
UIImage* myUIImage = [self newUIImageWithURLString:@"http://example.com/images/myimage.jpg"];
That's it!
It is important to note that +(NSData*) dataWithContentsOfURL:(NSURL*)url blocks. This will not do an asynchronous load. :( I am looking into another more involved solution that will load images on a separate thread.
This simple function will do all of the work. No UIWebView required no NSURLConnection required. Just dataWithContentsOfURL.
-(UIImage*) newUIImageWithURLString:(NSString*)urlString
{
return [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
}
Usage:
UIImage* myUIImage = [self newUIImageWithURLString:@"http://example.com/images/myimage.jpg"];
That's it!
It is important to note that +(NSData*) dataWithContentsOfURL:(NSURL*)url blocks. This will not do an asynchronous load. :( I am looking into another more involved solution that will load images on a separate thread.
Labels: Cocoa, iPhone, Objective-C, UIKit


0 Comments:
Post a Comment
<< Home