cocoa touch -为图像网格创建缩略图



我正在构建一个类似苹果在iPad上的照片应用程序的应用程序。我有一个大的全屏图像,我向他们展示了使用scrollView来管理缩放和分页。当我尝试用图像的缩略图创建网格时,主要的问题发生了。我将它们创建为UIImageView重叠在UIButton上。这一切都很好,但当我在iPad上试用这款应用时,它需要大量内存,我想这取决于图像的重新缩放。有没有办法用小图像创建一个UIImageView,重新缩放大图像,而不使用那么多内存?

您可以使用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();

最新更新