【问题标题】:Crop the bottom of a UIImage after resizing调整大小后裁剪 UIImage 的底部
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 你能具体说明一下代码中帧的哪一部分有错误的值吗?谢谢。
    • 底框与图片大小相同,但y位置不同。如果您想达到相同的结果,请检查此UIImage 类别:github.com/Adrian2112/UIImage-BlurredFrame
    【解决方案2】:

    很简单

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

    【讨论】:

      【解决方案3】:

      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
      }
      

      【讨论】:

      • 嗨@devjme 我使用了相同的功能,但它给了我一个横向图像而不是纵向图像。函数调用:UIImage* croppedImage = [self cropImage:image toRect:CGRectMake(self.capturedImageView.frame.size.width / 4, 0, self.capturedImageView.frame.size.width/2, self.capturedImageView.frame.size.height) viewWidth:self.capturedImageView.frame.size.width viewHeight:self.capturedImageView.frame.size.height]; 任何帮助将不胜感激。
      猜你喜欢
      • 2010-10-10
      • 2013-10-22
      • 2017-02-06
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 2014-01-11
      相关资源
      最近更新 更多