Cropping center square of UIImage

后端 未结 18 606
日久生厌
日久生厌 2020-12-07 23:09

So here\'s where I\'ve made it so far. I am using a UIImage captured from the camera and can crop the center square when in landscape. For some reason this doesn\'t transl

相关标签:
18条回答
  • 2020-12-07 23:34

    I think here would be the perfect solution!
    It is NOT good idea to crop image basis on the toSize's size. It will look weird when the image resolution (size) is very large.
    Following code will crop the image as per the toSize's ratio.
    Improved from @BlackRider's answer.

    - (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;
    }
    
    0 讨论(0)
  • 2020-12-07 23:34

    I was able to accomplish it with something like this:

       UIImage *thisImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
                    double x = (thisImage.size.width )/2.0;
                    double y = (thisImage.size.height)/2.0;
                    mediaImage = [self imageByCropping:thisImage toRect:CGRectMake(x, y, 60, 60)];
                    [thisImage release];
    
    0 讨论(0)
  • 2020-12-07 23:37

    there are already many solution, but i wanna post mine:

      extension UIImage {
    
        var cgImageWidth: Int { return cgImage?.width ?? 0 }
        var cgImageheight: Int { return cgImage?.height ?? 0 }
        var blance: CGFloat { return min(size.width, size.height)}
        var blanceSize: CGSize { return CGSize(width: blance, height: blance) }
        var blanceRect: CGRect { return CGRect(origin: .zero, size: blanceSize) }
    
        var roundedImage: UIImage? {
            UIGraphicsBeginImageContextWithOptions(blanceSize, false, scale)
            defer { UIGraphicsEndImageContext() }
            guard let cgImage = cgImage?.cropping(to: CGRect(origin: CGPoint(x: max(0, CGFloat(cgImageWidth) - blance)/2.0, y: max(0, CGFloat(cgImageheight) - blance)/2.0), size: blanceSize)) else { return nil }
            UIBezierPath(ovalIn: blanceRect).addClip()
    
            UIImage(cgImage: cgImage, scale: 1.0, orientation: self.imageOrientation).draw(in: blanceRect)
            return UIGraphicsGetImageFromCurrentImageContext()
        }
    
    }
    
    0 讨论(0)
  • 2020-12-07 23:38

    Swift 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
    
    0 讨论(0)
  • This is based on @Alonzo's Swift 3 answer above, with the code simplified and streamlined to make it more understandable and concise. It also bails out if the image is already square.

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-07 23:39

    Xamarin:

    UIImage ImageByCroppingImage(UIImage image, CGSize size)
    {
        double newCropWidth, newCropHeight;
    
        if (image.Size.Width < image.Size.Height)
        {
            newCropWidth = image.Size.Width < size.Width ? size.Width : image.Size.Width;
            newCropHeight = (newCropWidth * size.Height) / size.Width;
        }
        else
        {
            newCropHeight = image.Size.Height < size.Height ? size.Height : 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 = new CGRect(x, y, newCropWidth, newCropHeight);
        var imageRef = image.CGImage.WithImageInRect(cropRect);
    
        var cropped = new UIImage(imageRef);
    
        return cropped;
    }
    
    0 讨论(0)
提交回复
热议问题