我使用此代码调整图像大小。
- (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]; 保存它。
之后我加载文件,图像大小是原来的两倍,有什么提示吗?
谢谢!