我有这个图像(原始大小)
我想调整图像的大小,保持宽高比为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);
这是我得到的结果(我应该得到相同的图像,我想使该裁剪模糊底部裁剪区域以显示一些文本):
3 回答
This帮我解决了这个问题 . 我用错误的值初始化图像的帧 .
它非常简单
https://developer.apple.com/documentation/coregraphics/cgimage/1454683-cropping
这就是救了我的!!