【发布时间】:2013-11-23 20:25:53
【问题描述】:
我有这张图片(原始尺寸)
我想将图像的大小调整为 600 x 600 的纵横比。之后,我想只裁剪底部到此帧:{0, 400, 600, 200} 以获得图像的底部。
在保留纵横比的情况下调整大小效果很好,但我无法裁剪图像的底部。我正在使用此代码调整纵横比:
CGFloat horizontalRatio = bounds.width / self.size.width;
CGFloat verticalRatio = bounds.height / self.size.height;
CGFloat ratio;
switch (contentMode) {
case UIViewContentModeScaleAspectFill:
ratio = MAX(horizontalRatio, verticalRatio);
break;
case UIViewContentModeScaleAspectFit:
ratio = MIN(horizontalRatio, verticalRatio);
break;
default:
[NSException raise:NSInvalidArgumentException format:@"Unsupported content mode: %d", contentMode];
}
CGSize newSize = CGSizeMake(self.size.width * ratio, self.size.height * ratio);
UIImage *resizedImg = [self resizedImage:newSize interpolationQuality:quality];
if (UIViewContentModeScaleAspectFill == contentMode && (newSize.height > bounds.height || newSize.width > bounds.width)) {
CGRect newRect = CGRectMake((newSize.width - bounds.width) / 2, (newSize.height - bounds.height) / 2, bounds.width, bounds.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
return resizedImg;
这段代码用于裁剪底部
CGRect rect = CGRectMake(0, 400, 600, 200);
CGImageRef imageRef = CGImageCreateWithImageInRect([newImage CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
这就是我得到的结果(我应该得到相同的图像,我想让该裁剪模糊底部裁剪区域以显示一些文本):
【问题讨论】:
标签: ios core-graphics core-image