如何使用IOS輕松地調整/優化圖像大?。课业膽贸绦蛘趶木W絡下載一組圖像文件,并將它們保存到本地iPhone磁盤上。其中一些圖像的大小相當大(例如,寬度大于500像素)。由于iPhone甚至沒有足夠大的顯示器來顯示原來大小的圖像,所以我計劃將圖像調整到更小的位置,以節省空間/性能。此外,這些圖像中的一些是JPEG,它們不會像通常的60%質量設置那樣保存。如何使用iPhoneSDK調整圖片的大小,以及如何更改JPEG圖像的質量設置?
3 回答
一只甜甜圈
TA貢獻1836條經驗 獲得超5個贊
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;}NSData *dataForJPEGFile = UIImageJPEGRepresentation(theImage, 0.6);
元芳怎么了
TA貢獻1798條經驗 獲得超7個贊
float actualHeight = image.size.height;float actualWidth = image.size.width;float imgRatio = actualWidth/actualHeight;
float maxRatio = 320.0/480.0;if(imgRatio!=maxRatio){
if(imgRatio < maxRatio){
imgRatio = 480.0 / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = 480.0;
}
else{
imgRatio = 320.0 / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = 320.0;
}}CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];UIImage *img = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
達令說
TA貢獻1821條經驗 獲得超6個贊
CGImageSourceCreateThumbnailAtIndex
- (void)resizeImageAtPath:(NSString *)imagePath {
// Create the image source (from path)
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) [NSURL fileURLWithPath:imagePath], NULL);
// To create image source from UIImage, use this
// NSData* pngData = UIImagePNGRepresentation(image);
// CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)pngData, NULL);
// Create thumbnail options
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(id) kCGImageSourceThumbnailMaxPixelSize : @(640)
};
// Generate the thumbnail
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
CFRelease(src);
// Write the thumbnail at path
CGImageWriteToFile(thumbnail, imagePath);}- 3 回答
- 0 關注
- 937 瀏覽
