Image Cropping API for iOS

后端 未结 4 1370
一向
一向 2020-12-05 02:54

Is there any cropping image API for objective C that crops images dynamically in Xcode project? Please provide some tricks or techniques how could I crop camera images in iP

4条回答
  •  遥遥无期
    2020-12-05 03:27

    You can use below simple code to crop an image. You have to pass the image and the CGRect which is the cropping area. Here, I crop image so that I get center part of original image and returned image is square.

    // Returns largest possible centered cropped image.
    - (UIImage *)centerCropImage:(UIImage *)image
    {
        // Use smallest side length as crop square length
        CGFloat squareLength = MIN(image.size.width, image.size.height);
        // Center the crop area
        CGRect clippedRect = CGRectMake((image.size.width - squareLength) / 2, (image.size.height - squareLength) / 2, squareLength, squareLength);
    
        // Crop logic
        CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
        UIImage * croppedImage = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
        return croppedImage;
    }
    

提交回复
热议问题