有没有可以帮助我缩小图像的任何代码或库?如果您使用iPhone拍摄照片,则其像2000x1000像素,这不是非常网络友好的。我想把它缩小到480x320。任何提示?任何缩小UIImage的代码/库?
4
A
回答
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;
}
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;
}
+0
这是一个答案?它工作吗?我应该upvote它吗? – 2012-02-04 06:54:52
0
请参阅我发布到this question的解决方案。这个问题涉及将图像旋转90度而不是缩放,但前提是相同的(只是矩阵变换不同)。
2
请参阅http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ - 这有一组代码可以下载以及一些说明。
如果速度令人担忧,可以尝试使用CGContextSetInterpolationQuality来设置比默认值更低的插值质量。
相关问题
- 1. 缩小UIImage
- 2. 缩小代码
- 3. 如何缩小HTML代码?
- 4. 缩小UIImage的尺寸
- 5. JS缩小:压缩代码
- 6. 如何缩小你的jQuery代码?
- 7. 如何缩小Visual Studio代码中的JavaScript文件代码
- 8. 这段代码如何优化缩小?
- 9. 如何缩小此脚本代码
- 10. 如何压缩UIImage?
- 11. VB6源代码库的任何建议?
- 12. 在另一个js库中包含缩小的js代码
- 13. 未缩小的Google Analytics代码
- 14. Gulp:为缩小和非缩小脚本生成源代码
- 15. 任何库/类解码/编码Gzip,压缩,x-gzip或x-compress?
- 16. Gradle中的缩小任务
- 17. C#代码缩小工具和技巧
- 18. 钛加速器代码缩小
- 19. 简化或缩减代码大小
- 20. VBA缩短代码小于255字符
- 21. 如何使UIScrollView缩放为UIImage的大小?
- 22. Froala编辑器 - 不要缩小(浓缩/挤压)我的代码
- 23. 如何识别缩小的Google Closure代码中的课程?
- 24. 如何检查预编译的缩小的SASS代码
- 25. 任何使用Kdevelop自动缩进c/C++代码的方法?
- 26. 是否有任何iphone的解压缩代码?
- 27. 如何在部署前缩小我的源代码
- 28. 如何从缩小的JavaScript文件获取源代码?
- 29. symbian C++缩小库
- 30. 如何在UIImageView中自动缩放UIImage大小?
你为什么想缩放它?仅用于显示还是上传? – 2009-10-16 08:55:27