如何使用IOS輕松地調(diào)整/優(yōu)化圖像大???我的應用程序正在從網(wǎng)絡下載一組圖像文件,并將它們保存到本地iPhone磁盤上。其中一些圖像的大小相當大(例如,寬度大于500像素)。由于iPhone甚至沒有足夠大的顯示器來顯示原來大小的圖像,所以我計劃將圖像調(diào)整到更小的位置,以節(jié)省空間/性能。此外,這些圖像中的一些是JPEG,它們不會像通常的60%質量設置那樣保存。如何使用iPhoneSDK調(diào)整圖片的大小,以及如何更改JPEG圖像的質量設置?
3 回答

一只甜甜圈
TA貢獻1836條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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 關注
- 924 瀏覽
添加回答
舉報
0/150
提交
取消