为图像网格创建缩略图

时间:2011-04-28 12:51:30

标签: cocoa-touch ios uiimageview grid resize

我正在构建一个类似苹果iPad照片应用的应用。我有大型全屏图像,我使用scrollView显示它们来管理缩放和分页。当我尝试使用图像的缩略图创建网格时,会出现主要问题。我创建它们作为UIBmageView重叠在UIButton上。一切都很棒,但是当我在iPad上试用它时,它需要大量内存,我想这取决于图像的重新缩放。有一种方法可以用小图像创建UIImageView,重新缩放较大的图像,而不需要使用这么多的内存吗?

1 个答案:

答案 0 :(得分: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();