首页 文章

调整大小后裁剪UIImage的底部

提问于
浏览
0

我有这个图像(原始大小)

Image to resize

我想调整图像的大小,保持宽高比为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);

这是我得到的结果(我应该得到相同的图像,我想使该裁剪模糊底部裁剪区域以显示一些文本):

enter image description here

3 回答

  • 0

    This帮我解决了这个问题 . 我用错误的值初始化图像的帧 .

  • 0

    它非常简单

    - (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;
    }
    
  • 0

    https://developer.apple.com/documentation/coregraphics/cgimage/1454683-cropping

    这就是救了我的!!

    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
    }
    

相关问题