2012-02-02 103 views
1

我正在浏览以下tutorial。我需要获得大图像的缩略图。根据教程,下面的方法可以做到这一点。将图像显示到视图

CGImageRef MyCreateThumbnailImageFromData (NSData * data, int imageSize) 

注:看教程,如果你需要完整的代码,这种方法

我的问题是;

1.)我有一个URL http://www.myimage.com/smile.jpg,我需要调整它的缩略图。我不明白什么是NSData * data参数。所有我有的是一个字符串网址。那么如何以编程方式将我的URL传递给此方法?

2.)上述方法返回CGImageRef但我需要一个UIImage,所以我可以将它添加到一个UIIMageVIew,然后将其显示在我的项目中。那么我如何使用CGImageRef在我的项目中显示图像?

3.)我下载的图像非常大,2MB或更多。通过使它以缩略图大小显示,它是否会减少将图像加载到视图所用的时间?

+0

1&2只是编程问题,你会发现,但#3是一个更大的问题 - 你需要下载它来调整它的大小,但为什么要下载2MB的手机只是为了调整缩略图?取而代之的是服务器端或创建一个服务代理来执行并缓存它。 – bryanmac 2012-02-02 01:40:06

回答

3

让去通过此,一步一步:

1)一种NSData对象为字节,或char *的阵列的包装。这是您需要的图像的原始字节。

2)A CGImageRefCoreGraphics表示图像的方式,可以通过选择器+imageWithCGImage:转换为UIImage。一般来说,您可以通过CGImageRef更好地控制图像。

3)将这些图像转换为缩略图不会减少下载所需的时间。文件在转换之前必须首先下载到内存中。

如何使用您的函数示例:

int myImageSize = .... // do what you need to to figure out the size of the image 
UIImage *myImage = [UIImage imageWithCGImage:MyCreateThumbnailImageFromData([NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.myimage.com/smile.jpg"]], myImageSize)]; 

然而这将阻止用户界面,可以考虑使用,而不是-dataWithContentsOfURL: GCD或NSURLConnection的。

编辑:例如使用GCD:

dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
    __block UIImage *myImage = [UIImage imageWithCGImage:MyCreateThumbnailImageFromData([NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.myimage.com/smile.jpg"]], myImageSize)]; 

    [myImageView performSelectorOnMainThread:@selector(setImage:) withObject:myImage waitUntilDone:NO]; 
}); 
+1

对于#3考虑做它的服务器端或创建一个服务,如果可能的话缓存它。 – bryanmac 2012-02-02 01:41:00

+0

是的,但这不是OP中要求的。我确实同意他试图做到这一点的方式有点笨拙,但我曾与我给过的东西合作过。 – 2012-02-02 01:42:02

+0

+1 - 有道理。只是想补充答案。不知道这是否是他的选择... – bryanmac 2012-02-02 01:43:35

0
NSData* theData = [NSData dataWithContentsOfURL:@"http://www.myimage.com/smile.jpg"]; 
UIImage *theImage = [[UIImage alloc] initWithData:data]; 

UIGraphicsBeginImageContext(CGSizeMake(128, 96)); 
[theImage drawInRect:CGRectMake(0.0, 0.0, 128, 96)]; 
UIImage *myThumbnail = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

UIImageView *myThumbnailView = [[UIImageView alloc] initWithImage:myThumbnail]; 

编辑异步方法:

NSURLConnection* connection; 
NSMutableData* data; 

- (void)loadImageFromURL:(NSURL*)url; 
在实现文件

NSURL *url = [NSURL URLWithString:@"http://www.myimage.com/smile.jpg"]]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

- (void)loadImageFromURL:(NSURL*)url { 

    if (![NSThread isMainThread]) { 
     [self performSelectorOnMainThread:@selector(loadImageFromURL:) withObject:url waitUntilDone:NO]; 
     return; 
    } 
    if (connection!=nil) { [connection release]; } 

    if (data!=nil) { [data release]; } 

    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

} 

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



- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection { 

    [connection release]; 
    connection=nil; 

    UIImage *theImage = [[UIImage alloc] initWithData:data]; 

    UIGraphicsBeginImageContext(CGSizeMake(128, 96)); 
    [theImage drawInRect:CGRectMake(0.0, 0.0, 128, 96)]; 
    UIImage *myThumbnail = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageView *myThumbnailView = [[UIImageView alloc] initWithImage:myThumbnail]; 

    [data release]; 
    data=nil; 
} 

在你的头文件

+0

在上面的问题中,作者说使用'GCD或NSURLConnection代替-dataWithContentsOfURL:'。那么我怎么能在你的代码中做到这一点? – shajem 2012-02-02 14:32:00

+0

查看http://www.markj.net/iphone-asynchronous-table-image/获取数据的异步方法。 – ader 2012-02-02 15:17:39

+0

是的,我经历了它。该示例项目甚至没有运行 – shajem 2012-02-02 15:19:56