kCGImageSourceCreateThumbnailWithTransform
指定された Optionsのキーに値 (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);
この実装を変更して、サイズの代わりに必要な最大値を提供できます
ImageIO を使用してサイズを変更するために UIImage に追加されたカテゴリ
UIImage+Resizing.h
#import <UIKit/UIKit.h>
@interface UIImage (Resizing)
-(UIImage*)resizedImageWithData:(NSData*)imageData withSize:(CGSize)size;
@end
UIImage+Resizing.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