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.
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.
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.
have a look at here
[DLImageLoader loadImageFromURL:imageURL
completed:^(NSError *error, NSData *imgData) {
imageView.image = [UIImage imageWithData:imgData];
[imageView setContentMode:UIViewContentModeCenter];
}];
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.