如何在两个维度上将UIImage平均缩小一半?
我有一个UIImage,它的大小是UIImageView的两倍,并希望它的大小相同,而不使用任何内容模式进行填充或缩放等。
答案 0 :(得分:6)
让自己成为一个新形象(表示为UIImage的类别方法):
- (UIImage*)scaleToSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), self.CGImage);
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}