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



1 Comments:
Have you fount a way to make asynchronous loads of images?
hi can you give me a smaple code or a tutorial on how to do this my email is adamwweraw@hotmail.com
Post a Comment
Links to this post:
Create a Link
<< Home