那里有没有可以帮助我缩小图像的代码或库?如果你用iPhone拍照,它就像2000x1000像素,不是很友好。我想把它缩小到480x320。任何提示?
答案 0 :(得分:8)
这就是我正在使用的。效果很好。我肯定会看到这个问题,看看是否有人有更好/更快的东西。我刚刚将以下内容添加到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;
}
答案 1 :(得分:2)
请参阅http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ - 这里有一组您可以下载的代码以及一些说明。
如果担心速度,您可以尝试使用CGContextSetInterpolationQuality设置比默认值更低的插值质量。
答案 2 :(得分:0)
请注意,这不是我的代码。我做了一点挖掘,发现它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;
}
答案 3 :(得分:0)
请参阅我发布到this question的解决方案。问题涉及将图像旋转90度而不是缩放它,但前提是相同的(只是矩阵变换不同)。