【问题标题】:Creating thumbnail for an image grid为图像网格创建缩略图
【发布时间】:2011-04-28 12:51:30
【问题描述】:

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

【问题讨论】:

    标签: cocoa-touch ios uiimageview grid resize


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 2017-03-19
      • 2016-07-02
      • 2018-10-31
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 2016-04-19
      相关资源
      最近更新 更多