kCGImageSourceCreateThumbnailWithTransform
为提供给的选项中的键提供值 (kCFBooleanTrue),CGImageSourceCreateThumbnailAtIndex
缩略图应根据完整图像的方向和像素纵横比进行旋转和缩放。
这是此密钥的文档
/* Specifies whether the thumbnail should be rotated and scaled according
* to the orientation and pixel aspect ratio of the full image. The value
* of this key must be a CFBooleanRef; the default value of this key is
* kCFBooleanFalse. */
IMAGEIO_EXTERN const CFStringRef kCGImageSourceCreateThumbnailWithTransform IMAGEIO_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_4_0);
您可以修改此实现以提供所需的最大值而不是大小
添加到 UIImage 的类别以使用 ImageIO 调整其大小
UIImage+调整大小.h
#import <UIKit/UIKit.h>
@interface UIImage (Resizing)
-(UIImage*)resizedImageWithData:(NSData*)imageData withSize:(CGSize)size;
@end
UIImage+调整大小.m
#import "UIImage+Resizing.h"
#import <ImageIO/ImageIO.h>
@implementation UIImage (Resizing)
-(UIImage*)resizedImageWithData:(NSData*)imageSource withSize:(CGSize)size{
// Resizing Using ImageIO
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageSource, nil);
// load the image at the desired size
NSDictionary* options = @{
(id)kCGImageSourceShouldAllowFloat: (id)kCFBooleanTrue,
(id)kCGImageSourceCreateThumbnailWithTransform: (id)kCFBooleanTrue,
(id)kCGImageSourceCreateThumbnailFromImageAlways: (id)kCFBooleanTrue,
(id)kCGImageSourceThumbnailMaxPixelSize: @((int)(size.width > size.height ? size.width : size.height))
};
CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(src, 0, (__bridge CFDictionaryRef)options);
if (NULL != src)
CFRelease(src);
UIImage* scaledImage = [UIImage imageWithCGImage:imageRef];
if (NULL != imageRef)
CFRelease(imageRef);
return scaledImage;
}
@end