2011-04-28 154 views
0

我正在iPad上構建一個應用程序,例如蘋果的照片應用程序。我有很大的全屏幕圖像,我使用scrollView來顯示它們來管理縮放和分頁。當我嘗試使用圖像的縮略圖創建網格時,會出現主要問題。我將它們創建爲在UIButton上重疊的UIImageView。所有的作品都很棒,但是當我在iPad上試用這款應用程序時,它需要大量的內存,我想這取決於Image的縮放比例。有一種方法可以使用小圖像創建UIImageView,重新縮放較大的圖像,而不使用太多的內存?爲圖像網格創建縮略圖

回答

0

您可以使用UIGraphics創建縮略圖。這裏是這樣做的代碼:

UIGraphicsBeginImageContext(CGSizeMake(length, length)); 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
CGContextClipToRect(currentContext, clippedRect); 
CGFloat scaleFactor = length/sideFull; 
if (widthGreaterThanHeight) { 
    //a landscape image – make context shift the original image to the left when drawn into the context 
    CGContextTranslateCTM(currentContext, -((mainImage.size.width - sideFull)/2) * scaleFactor, 0); 
} 
else { 
    //a portfolio image – make context shift the original image upwards when drawn into the context 
    CGContextTranslateCTM(currentContext, 0, -((mainImage.size.height - sideFull)/2) * scaleFactor); 
} 
//this will automatically scale any CGImage down/up to the required thumbnail side (length) when the CGImage gets drawn into the context on the next line of code 
CGContextScaleCTM(currentContext, scaleFactor, scaleFactor); 
[mainImageView.layer renderInContext:currentContext]; 
UIImage* thumbnail = UIGraphicsGetImageFromCurrentImageContext();