本文介绍了适用于iOS的Image Cropping API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何裁剪图像API用于在Xcode项目中动态裁剪图像的目标C?请提供一些技巧或技巧如何在iPhone中裁剪相机图像。
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 iPhone.
推荐答案
您可以使用以下简单代码裁剪图像。您必须传递图像和作为裁剪区域的CGRect。在这里,我裁剪图像,以便获得原始图像的中心部分,返回的图像是正方形。
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;
}
这篇关于适用于iOS的Image Cropping API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!