このコードを使用して画像のサイズを変更します。
- (UIImage*)resizeToSize:(CGSize)size {
float height = self.size.height;
float width = self.size.width;
if (width > size.width) {
width = size.width;
height = size.width / (self.size.width / self.size.height);
}
if (height > size.height) {
height = size.height;
width = size.height / (self.size.height / self.size.width);
}
NSLog(@"Resize to size %@",NSStringFromCGSize(size));
if (height == self.size.height && width == self.size.width) {
return self;
}
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContextWithOptions(newSize, YES, 0.0);
[self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"Resized %@",NSStringFromCGSize(newImage.size));
return newImage;
}
画像のサイズが変更され、次のステップで [UIImageJPEGRepresentation(image, 1.0) writeToFile:pngPath atomically:YES]; で保存します。
その後、ファイルをロードすると、画像サイズが 2 倍になります。
ありがとうございました!