【问题标题】:Any code/library to scale down an UIImage?任何代码/库来缩小 UIImage?
【发布时间】:2010-12-07 02:56:14
【问题描述】:

是否有任何代码或库可以帮助我缩小图像?如果你用 iPhone 拍照,它大概是 2000x1000 像素,对网络不太友好。我想将其缩小为 480x320。有什么提示吗?

【问题讨论】:

  • 为什么要缩放它?是展示还是上传?

标签: iphone graphics


【解决方案1】:

这就是我正在使用的。效果很好。我肯定会看这个问题,看看是否有人有更好/更快的东西。我刚刚将以下内容添加到 UIimage 的类别中。

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
  UIGraphicsBeginImageContext( newSize );
  [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return newImage;
}

【讨论】:

  • 那么如果比率不同会怎样? drawInRect 会做什么?说原始是 2000x1000,我通过了 480x480
  • 来自 UIImage 上的开发者文档 - “在指定的矩形中绘制整个图像,并根据需要对其进行缩放以适合。”
  • 此方法需要很长时间才能运行,有时超过一分钟。我将 iPhone 3GS 相机拍摄的照片缩小到 500x500。为什么我想知道?
  • 我注意到这需要一段时间(但对我来说,它是 4-8 秒,而不是超过一分钟),这就是为什么我在看其他答案......我打算对它们进行基准测试。
  • 为什么一张图片缩放成UIImageView需要
【解决方案2】:

参见http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ - 这里有一组代码和一些说明可供下载。

如果担心速度,您可以尝试使用 CGContextSetInterpolationQuality 设置低于默认值的插值质量。

【讨论】:

    【解决方案3】:

    请注意,这不是我的代码。我做了一点挖掘,发现它here。我认为您必须进入 CoreGraphics 层,但不太确定具体细节。这应该有效。只是要小心管理你的记忆。

    //  ==============================================================
    //  resizedImage
    //  ==============================================================
    // Return a scaled down copy of the image.  
    
    UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
    {
        CGImageRef          imageRef = [inImage CGImage];
        CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);
    
        // There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
        // see Supported Pixel Formats in the Quartz 2D Programming Guide
        // Creating a Bitmap Graphics Context section
        // only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
        // and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
        // The images on input here are likely to be png or jpeg files
        if (alphaInfo == kCGImageAlphaNone)
            alphaInfo = kCGImageAlphaNoneSkipLast;
    
        // Build a bitmap context that's the size of the thumbRect
        CGContextRef bitmap = CGBitmapContextCreate(
                    NULL,
                    thumbRect.size.width,       // width
                    thumbRect.size.height,      // height
                    CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
                    4 * thumbRect.size.width,   // rowbytes
                    CGImageGetColorSpace(imageRef),
                    alphaInfo
            );
    
        // Draw into the context, this scales the image
        CGContextDrawImage(bitmap, thumbRect, imageRef);
    
        // Get an image from the context and a UIImage
        CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
        UIImage*    result = [UIImage imageWithCGImage:ref];
    
        CGContextRelease(bitmap);   // ok if NULL
        CGImageRelease(ref);
    
        return result;
    }
    

    【讨论】:

      【解决方案4】:

      请查看我发布到this question 的解决方案。问题涉及将图像旋转 90 度而不是对其进行缩放,但前提是相同的(只是矩阵变换不同)。

      【讨论】:

        猜你喜欢
        • 2012-12-07
        • 1970-01-01
        • 1970-01-01
        • 2011-08-14
        • 2010-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多