UIImage的裁剪中心广场

Uxo*_*ith 30 image uiimage ios

所以这就是我到目前为止所做的.我正在使用从相机捕获的UIImage,并且可以在景观中裁剪中心广场.由于某种原因,这不会像预期的那样转换为纵向模式.我将发布我的代码和日志仅供参考.

码:

CGRect squareRect = CGRectMake(offsetX, offsetY, newWidth, newHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], squareRect);
image = [UIImage imageWithCGImage:imageRef scale:1 orientation:image.imageOrientation];
Run Code Online (Sandbox Code Playgroud)

人像结果(不是正方形):

original image size: {1536, 2048}, with orientation: 3
squareRect: {{0, 256}, {1536, 1536}}
new image size: {1280, 1536}, with orientation: 3 <--- not expected
Run Code Online (Sandbox Code Playgroud)

景观结果(方形):

original image size: {2048, 1536}, with orientation: 1
squareRect: {{256, 0}, {1536, 1536}}
new image size: {1536, 1536}, with orientation: 1
Run Code Online (Sandbox Code Playgroud)

这是CGImageCreateWithImageInRect()中的错误还是我在这里遗漏了什么?

Nir*_*ngi 26

我认为这将是完美的解决方案!
它是不是好主意,裁剪图像基础上toSize大小.当图像分辨率(大小)非常大时,它看起来很奇怪.
下面的代码将裁剪图像具体根据toSize比率.
改进了@BlackRider的答案.

- (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size
{
    double newCropWidth, newCropHeight;

    //=== To crop more efficently =====//
    if(image.size.width < image.size.height){
         if (image.size.width < size.width) {
                 newCropWidth = size.width;
          }
          else {
                 newCropWidth = image.size.width;
          }
          newCropHeight = (newCropWidth * size.height)/size.width;
    } else {
          if (image.size.height < size.height) {
                newCropHeight = size.height;
          }
          else {
                newCropHeight = image.size.height;
          }
          newCropWidth = (newCropHeight * size.width)/size.height;
    }
    //==============================//

    double x = image.size.width/2.0 - newCropWidth/2.0;
    double y = image.size.height/2.0 - newCropHeight/2.0;

    CGRect cropRect = CGRectMake(x, y, newCropWidth, newCropHeight);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);

    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return cropped;
}
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的解决方案. (2认同)
  • 最好的解决方案!效果很好.谢谢.(Y) (2认同)

小智 24

这适用于不同的方向.纵向和横向都可以正常工作.

- (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size
{
    // not equivalent to image.size (which depends on the imageOrientation)!
    double refWidth = CGImageGetWidth(image.CGImage);
    double refHeight = CGImageGetHeight(image.CGImage);

    double x = (refWidth - size.width) / 2.0;
    double y = (refHeight - size.height) / 2.0;

    CGRect cropRect = CGRectMake(x, y, size.height, size.width);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);

    UIImage *cropped = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:self.imageOrientation];
    CGImageRelease(imageRef);

    return cropped;
}
Run Code Online (Sandbox Code Playgroud)


小智 16

基于Elmundo的检查答案及其Imbrue的快速版本,这里是自动计算图像中心大小的相同解决方案(考虑方向),并考虑到错误:

func cropImageToSquare(image: UIImage) -> UIImage? {
    var imageHeight = image.size.height
    var imageWidth = image.size.width

    if imageHeight > imageWidth {
        imageHeight = imageWidth
    }
    else {
        imageWidth = imageHeight
    }

    let size = CGSize(width: imageWidth, height: imageHeight)

    let refWidth : CGFloat = CGFloat(CGImageGetWidth(image.CGImage))
    let refHeight : CGFloat = CGFloat(CGImageGetHeight(image.CGImage))

    let x = (refWidth - size.width) / 2
    let y = (refHeight - size.height) / 2

    let cropRect = CGRectMake(x, y, size.height, size.width)
    if let imageRef = CGImageCreateWithImageInRect(image.CGImage, cropRect) {
        return UIImage(CGImage: imageRef, scale: 0, orientation: image.imageOrientation)
    }

   return nil
}
Run Code Online (Sandbox Code Playgroud)

Swift 3

func cropImageToSquare(image: UIImage) -> UIImage? {
    var imageHeight = image.size.height
    var imageWidth = image.size.width

    if imageHeight > imageWidth {
        imageHeight = imageWidth
    }
    else {
        imageWidth = imageHeight
    }

    let size = CGSize(width: imageWidth, height: imageHeight)

    let refWidth : CGFloat = CGFloat(image.cgImage!.width)
    let refHeight : CGFloat = CGFloat(image.cgImage!.height)

    let x = (refWidth - size.width) / 2
    let y = (refHeight - size.height) / 2

    let cropRect = CGRect(x: x, y: y, width: size.height, height: size.width)
    if let imageRef = image.cgImage!.cropping(to: cropRect) {
        return UIImage(cgImage: imageRef, scale: 0, orientation: image.imageOrientation)
    }

    return nil
}
Run Code Online (Sandbox Code Playgroud)


Mac*_*tle 13

尝试这样的事情:

- (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size
{
    double x = (image.size.width - size.width) / 2.0;
    double y = (image.size.height - size.height) / 2.0;

    CGRect cropRect = CGRectMake(x, y, size.height, size.width);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);

    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return cropped;
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我没有在通话中指定方向UIImage imageWithCGImage:.我想知道你的代码中是否存在问题.


Kev*_*OUX 8

已检查的解决方案有快速版本:

private func imageByCroppingImage(image : UIImage, size : CGSize) -> UIImage{
        var refWidth : CGFloat = CGFloat(CGImageGetWidth(image.CGImage))
        var refHeight : CGFloat = CGFloat(CGImageGetHeight(image.CGImage))

        var x = (refWidth - size.width) / 2
        var y = (refHeight - size.height) / 2

        let cropRect = CGRectMake(x, y, size.height, size.width)
        let imageRef = CGImageCreateWithImageInRect(image.CGImage, cropRect)

        let cropped : UIImage = UIImage(CGImage: imageRef, scale: 0, orientation: image.imageOrientation)!


        return cropped
    }
Run Code Online (Sandbox Code Playgroud)


Tul*_*leb 6

雨燕5

extension UIImage {

    var squared: UIImage? {
        guard let cgImage = cgImage else {
            return nil
        }

        let length = min(cgImage.width, cgImage.height)

        let x = cgImage.width / 2 - length / 2
        let y = cgImage.height / 2 - length / 2
        let cropRect = CGRect(x: x, y: y, width: length, height: length)

        guard let croppedCGImage = cgImage.cropping(to: cropRect) else {
            return nil
        }

        return UIImage(cgImage: croppedCGImage, scale: scale, orientation: imageOrientation)
    }
}
Run Code Online (Sandbox Code Playgroud)


srj*_*ang 5

从@ Elmundo的回答中得到了改进

+(UIImage *)getCenterMaxSquareImageByCroppingImage:(UIImage *)image withOrientation:(UIImageOrientation)imageOrientation

{
    CGSize centerSquareSize;
    double oriImgWid = CGImageGetWidth(image.CGImage);
    double oriImgHgt = CGImageGetHeight(image.CGImage);
    NSLog(@"oriImgWid==[%.1f], oriImgHgt==[%.1f]", oriImgWid, oriImgHgt);
    if(oriImgHgt <= oriImgWid) {
        centerSquareSize.width = oriImgHgt;
        centerSquareSize.height = oriImgHgt;
    }else {
        centerSquareSize.width = oriImgWid;
        centerSquareSize.height = oriImgWid;
    }

    NSLog(@"squareWid==[%.1f], squareHgt==[%.1f]", centerSquareSize.width, centerSquareSize.height);

    double x = (oriImgWid - centerSquareSize.width) / 2.0;
    double y = (oriImgHgt - centerSquareSize.height) / 2.0;
    NSLog(@"x==[%.1f], x==[%.1f]", x, y);

    CGRect cropRect = CGRectMake(x, y, centerSquareSize.height, centerSquareSize.width);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);

    UIImage *cropped = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:imageOrientation];
    CGImageRelease(imageRef);


    return cropped;
}
Run Code Online (Sandbox Code Playgroud)


Geo*_*rey 5

Swift 2 使用 UIImage 扩展

extension UIImage
{    
    func imageByCroppingImage(size : CGSize) -> UIImage
    {
        let refWidth : CGFloat = CGFloat(CGImageGetWidth(self.CGImage))
        let refHeight : CGFloat = CGFloat(CGImageGetHeight(self.CGImage))

        let x = (refWidth - size.width) / 2
        let y = (refHeight - size.height) / 2

        let cropRect = CGRectMake(x, y, size.width, size.height)
        let imageRef = CGImageCreateWithImageInRect(self.CGImage, cropRect)

        let cropped : UIImage = UIImage(CGImage: imageRef!, scale: 0, orientation: self.imageOrientation)


        return cropped
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

雨燕3

    let refWidth : CGFloat = CGFloat(image.cgImage!.width)
    let refHeight : CGFloat = CGFloat(image.cgImage!.height)

    let x = (refWidth - size.width) / 2
    let y = (refHeight - size.height) / 2

    let cropRect = CGRect(x: x, y: y, width: size.width, height: size.height)
    let imageRef = image.cgImage!.cropping(to: cropRect)

    let cropped : UIImage = UIImage(cgImage: imageRef!, scale: 0, orientation: image.imageOrientation)


    return cropped
Run Code Online (Sandbox Code Playgroud)

  • 您能否在您的答案中添加一些解释(关于OP做错了什么),而不仅仅是代码转储? (2认同)

pha*_*ann 5

这是基于上面 @Alonzo 的 Swift 3 答案,代码经过简化和精简,使其更易于理解和简洁。如果图像已经是方形的,它也会退出。

extension UIImage {
    func square() -> UIImage? {
        if size.width == size.height {
            return self
        }

        let cropWidth = min(size.width, size.height)

        let cropRect = CGRect(
            x: (size.width - cropWidth) * scale / 2.0,
            y: (size.height - cropWidth) * scale / 2.0,
            width: cropWidth * scale,
            height: cropWidth * scale
        )

        guard let imageRef = cgImage?.cropping(to: cropRect) else {
            return nil
        }

        return UIImage(cgImage: imageRef, scale: scale, orientation: imageOrientation)
    }
}
Run Code Online (Sandbox Code Playgroud)