Ios 调整大小后裁剪UIImage的底部
我有这张照片(原版尺寸) 我想调整图像的大小,将纵横比保持在600 x 600。在那之后,我只想裁剪这一帧的底部:Ios 调整大小后裁剪UIImage的底部,ios,core-graphics,core-image,Ios,Core Graphics,Core Image,我有这张照片(原版尺寸) 我想调整图像的大小,将纵横比保持在600 x 600。在那之后,我只想裁剪这一帧的底部:{0400600200},以获得图像的底部 通过保留纵横比调整大小效果很好,但我无法裁剪图像的底部。我使用以下代码根据纵横比调整大小: CGFloat horizontalRatio = bounds.width / self.size.width; CGFloat verticalRatio = bounds.height / self.size.height; CGFloat
{0400600200}
,以获得图像的底部
通过保留纵横比调整大小效果很好,但我无法裁剪图像的底部。我使用以下代码根据纵横比调整大小:
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);
这就是我得到的结果(我应该得到相同的图像,我想使该裁剪模糊底部裁剪区域以显示一些文本):
帮助我解决了这个问题。我用错误的值初始化图像的帧。非常简单
- (UIImage *)cropImage:(UIImage *)oldImage {
CGSize imageSize = oldImage.size;
UIGraphicsBeginImageContextWithOptions( CGSizeMake( imageSize.width,
imageSize.height - 200),
NO,
0.);
[oldImage drawAtPoint:CGPointMake( 0, -100)
blendMode:kCGBlendModeCopy
alpha:1.];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage;
}
这就是救我的原因
func cropImage(_ inputImage: UIImage, toRect cropRect: CGRect, viewWidth: CGFloat, viewHeight: CGFloat) -> UIImage?
{
let imageViewScale = max(inputImage.size.width / viewWidth,
inputImage.size.height / viewHeight)
// Scale cropRect to handle images larger than shown-on-screen size
let cropZone = CGRect(x:cropRect.origin.x * imageViewScale,
y:cropRect.origin.y * imageViewScale,
width:cropRect.size.width * imageViewScale,
height:cropRect.size.height * imageViewScale)
// Perform cropping in Core Graphics
guard let cutImageRef: CGImage = inputImage.cgImage?.cropping(to:cropZone)
else {
return nil
}
// Return image to UIImage
let croppedImage: UIImage = UIImage(cgImage: cutImageRef)
return croppedImage
}
您能否具体说明帧的哪一部分在代码中有错误的值?谢谢。底部框架的图像大小相同,但位置不同。如果你想获得相同的结果,请检查这个
UIImage
category:Hi@devjme我使用了相同的函数,但它给了我一个横向图像而不是纵向图像。函数调用:UIImage*cropedimage=[self-cropImage:image-toRect:CGRectMake(self.capturedImageView.frame.size.width/4,0,self.capturedImageView.frame.size.width/2,self.capturedImageView.frame.size.width)viewWidth:self.capturedImageView.frame.size.height]代码>任何帮助都将不胜感激。