将图像从url加载到uiimageview会降低iphone应用程序的性能
我正在为iphone开发一个rss提要应用程序,因为我有一个tableview,当用户单击标题时,所有提要标题都将显示,而详细视图将显示标题,描述和图像,但我面临性能问题,因为应用程序需要时间在将图像从url加载到uiimageview会降低iphone应用程序的性能,iphone,objective-c,uiimageview,Iphone,Objective C,Uiimageview,我正在为iphone开发一个rss提要应用程序,因为我有一个tableview,当用户单击标题时,所有提要标题都将显示,而详细视图将显示标题,描述和图像,但我面临性能问题,因为应用程序需要时间在UIImageView中加载图像,因为图像来自下面的图像url是我的代码 NSURL *url = [NSURL URLWithString:imageURL]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImageView *subview
UIImageView
中加载图像,因为图像来自下面的图像url是我的代码
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];
在这段代码中,imageurl来自rss并处理到UIImageView
以显示图像。如果我删除这个特定的代码,性能会很好,因为所有数据都是文本
因此,如何从url获取图像并快速显示到
UIImageView
控件,而不损失性能或显示活动指示器,直到图像完全单独加载。请帮我解决这个问题。不要在主线程上执行网络调用。苹果为此提供了一个有用的示例应用程序。有关在后台加载图像的正确方法,请参阅LazyTableImages
示例应用。切勿在主线程上执行网络调用。苹果为此提供了一个有用的示例应用程序。有关在后台加载图像的正确方法,请参阅LazyTableImages
示例应用程序。您好,您还可以使用名为
或者您可以创建自己的类,该类具有NSURLConnectionLegate
当用户界面因同步url请求而被锁定时,这些解决方案也可以解决您的问题。您好,您还可以使用第三方类解决您的问题,该类名为 或者您可以创建自己的类,该类具有NSURLConnectionLegate 当用户界面因同步url请求而锁定时,这些解决方案也可以解决您的问题。您可以尝试此解决方案
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];
}
谢谢你可以试试这个
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];
}
谢谢你可以试试这个
//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];
}
希望这对你有帮助。你可以试试这个
//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];
}
希望这能对你有所帮助。看看
看看
请固定键盘上的shift键。:)请固定键盘上的shift键。:)这是一个非常有用的图书馆。(1) 下载项目中的文件(2)drop(3)使用如上所示的命令。这个库吃掉了所有其他库,用于图像下载。。如此优雅,如此坚实。它做任何事情,缓存,工作。如此简单…………这只是一个非常有用的库。(1) 下载项目中的文件(2)drop(3)使用如上所示的命令。这个库吃掉了所有其他库,用于图像下载。。如此优雅,如此坚实。它做任何事情,缓存,工作。这么简单。。。。。。。。。。。