如何从iPhone的网址快速显示图像

时间:2012-06-13 11:58:09

标签: iphone ios5

我的项目基于评分图片。我想知道从链接快速在imageview中显示图像的最佳方法,没有内存泄漏,也没有任何崩溃。因为,将来图像可以是数十万的数量级,我将不得不显示这些图像。 是否有必要先下载图片???如果有人了解它,那么请给我一些解决方案。

提前感谢所有人。

1 个答案:

答案 0 :(得分:0)

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

  

[yourAsynchronousImageView   loadImageFromURLString:@ “HTTP://your.url/image.jpg”];

#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