如何在iphone中快速显示url中的图像

如何在iphone中快速显示url中的图像,iphone,ios5,Iphone,Ios5,我的项目是基于评级图片。我想知道从链接在imageview中快速显示图像的最佳方式,而不会出现内存泄漏和崩溃。因为,在未来的图像可以在数十万的顺序,我将不得不显示这些图像。 有必要先下载图片吗???如果有人对此有所了解,那么请为我提供一些解决方案 多亏了all Prevance。你可以从互联网上异步下载图片,从而保持应用程序的响应速度。已经有了一个解决方案,例如通过调用该方法 [yourAsynchronousImageView] loadImageFromURLString:@“http://

我的项目是基于评级图片。我想知道从链接在imageview中快速显示图像的最佳方式,而不会出现内存泄漏和崩溃。因为,在未来的图像可以在数十万的顺序,我将不得不显示这些图像。 有必要先下载图片吗???如果有人对此有所了解,那么请为我提供一些解决方案


多亏了all Prevance。

你可以从互联网上异步下载图片,从而保持应用程序的响应速度。已经有了一个解决方案,例如通过调用该方法

[yourAsynchronousImageView] loadImageFromURLString:@“http://your.url/image.jpg"];


使用延迟加载图像,因为它的工作方式与图像可用时的工作方式类似。如果不下载图像,您将如何从internet显示图像???好的,这意味着下载图像很重要。只要光束技术尚未发明,您就必须下载它。
#import <UIKit/UIKit.h>

@interface AsynchronousImageView : UIImageView {
    NSURLConnection *connection;    
    NSMutableData *data;
    bool loading;
}  

@property bool loading;

- (void)loadImageFromURLString:(NSString *)theUrlString;

@end
#import "AsynchronousImageView.h"

@implementation AsynchronousImageView
@synthesize loading;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)loadImageFromURLString:(NSString *)theUrlString{ 
    self.image = nil;
    NSURLRequest *request = [NSURLRequest requestWithURL:
                             [NSURL URLWithString:theUrlString]
                                             cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                         timeoutInterval:30.0];                          
    connection = [[NSURLConnection alloc]
                  initWithRequest:request delegate:self];
    loading=true;
}

- (void)connection:(NSURLConnection *)theConnection
    didReceiveData:(NSData *)incrementalData {    if (data == nil)
        data = [[NSMutableData alloc] initWithCapacity:2048];                 

    [data appendData:incrementalData];}

- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {    
    self.image = [UIImage imageWithData:data];
    data = nil; 
    connection = nil;
    loading=false;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end