2012-02-02 104 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