Reputation: 35
am developing an rss feed app for iphone, in that i have a tableview where all feed titles will show when user click on title a detail view will show with title, description and image but am facing problem with performance since the application taking time to load image in UIImageView
since the image is coming from image url below is my code.
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(110,10,100,80)];
[subview setImage:[UIImage imageWithData:data]];
[cell addSubview:subview];
[subview release];
in this code imageurl is coming from rss and processing to UIImageView
to show image. if i remove this specific code the performance is good since all data is text.
so how can i get image from url and display to UIImageView
control very quickly without losing performance or show activity indicator until image load completly separately. please help me for this.
Upvotes: 0
Views: 2700
Reputation: 5331
have a look at here
[DLImageLoader loadImageFromURL:imageURL
completed:^(NSError *error, NSData *imgData) {
imageView.image = [UIImage imageWithData:imgData];
[imageView setContentMode:UIViewContentModeCenter];
}];
Upvotes: 1
Reputation: 619
you can try this
//in .h file
IBOutlet UIImageView *imgTest;
-(IBAction)buttonTapped:(id)sender;
-(void)LoadImage:(NSString *) irlString;
-(void)setImage:(NSData *) imgData;
//in .m file write the following code:
-(IBAction)buttonTapped:(id)sender
{
[self performSelectorOnMainThread:@selector(LoadImage:) withObject:@"http://www.google.com/images/errors/logo_sm.gif" waitUntilDone:NO];
}
-(void)LoadImage:(NSString *) urlString
{
NSURL *imgURL=[NSURL URLWithString:urlString];
NSData *imgData=[NSData dataWithContentsOfURL:imgURL];
[self performSelectorInBackground:@selector(setImage:) withObject:imgData];
}
-(void)setImage:(NSData *) imgData;
{
imgTest.image=[UIImage imageWithData:imgData];
}
hope this will help you.
Upvotes: -1
Reputation: 2914
you can try this one,
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(void) {
[self loadImage];
});
-(void) loadImage
{
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(110,10,100,80)];
[subview setImage:[UIImage imageWithData:data]];
[cell addSubview:subview];
[subview release];
}
thanks
Upvotes: 0
Reputation: 372
Hi you can also use a third party Class for you problem called AsyncImageView
Or you can create your own class that have NSURLConnectionDelegate that downloads an image
These solutions can also solve your problems when the UI is locking because of synchronous url request.
Upvotes: 1
Reputation: 318804
Never perform network calls on the main thread. Apple provides a helpful example app for this. See the LazyTableImages
sample app for the proper way to load images in the background.
Upvotes: 3