2011-04-28 162 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();