如何使用IOS轻松地调整/优化图像大小?我的应用程序正在从网络下载一组图像文件,并将它们保存到本地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 关注
- 1010 浏览
添加回答
举报
0/150
提交
取消
