有沒有可以幫助我縮小圖像的任何代碼或庫?如果您使用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