简体   繁体   English

如何在iPhone中裁剪图像?

[英]How to crop image in iPhone?

In my application I m using following codes to crop the captured image :- 在我的应用程序中,我使用以下代码裁剪捕获的图像:-

-(void)imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{   
#ifdef _DEBUG
    NSLog(@"frmSkinImage-imagePickerController-Start");
#endif

    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    //=======================================
    UIImage *image =imageView.image;
    CGRect cropRect = CGRectMake(100, 100, 125,128);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
    [imageView setImage:[UIImage imageWithCGImage:imageRef]]; 
    CGImageRelease(imageRef);
    //===================================================
    //imgglobal = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    // for saving image to photo album
    //UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), self);
    [picker dismissModalViewControllerAnimated:YES];

#ifdef _DEBUG
    NSLog(@"frmSkinImage-imagePickerController-End");
#endif
}

But my problem is that when I use camera to take photo to crop the captured image it rotates the image to 90 degree towards right and in case I use Photo library it works perfectly. 但是我的问题是,当我使用相机拍摄照片以裁剪捕获的图像时,它会将图像向右旋转90度,并且如果使用照片库,它可以完美地工作。

So can you examine my above code, to see where I am wrong? 那么,您可以检查一下我上面的代码,看看我错了吗?

The CGImage is a UIImage without the meta data, and therefore loses the orientation information. CGImage是没有元数据的UIImage,因此会丢失方向信息。 I'd suggest that you get the orientation of the original [UIImage imageOrientation], store it and then apply it to the final image. 我建议您获取原始[UIImage imageOrientation]的方向,将其存储,然后将其应用于最终图像。

If that doesn't work, try applying a CGAffineTransformMakeRotation(90.0*0.0174532925); 如果这不起作用,请尝试应用CGAffineTransformMakeRotation(90.0*0.0174532925); to the final image according to the orientation of the original. 根据原始图像的方向调整到最终图像。

Fastest and easiest way is to have a look to NYXImagesKit . 最快最简单的方法是查看NYXImagesKit With UIImage+Resizing category you can crop image. 使用UIImage+Resizing类别,您可以裁剪图像。 From the documentation: 从文档中:

UIImage+Resizing UIImage +调整大小

This category can be used to crop or to scale images. 此类别可用于裁剪或缩放图像。

Cropping 播种

You can crop your image by 9 different ways : 您可以通过9种不同的方式裁剪图像:

Top left Top center Top right Bottom left Bottom center Bottom right Left center Right center Center 左上方顶部中心右上方左下方底部中心右下方左中心右中心中心

UIImage* cropped = [myImage cropToSize:(CGSize){width, height} usingMode:NYXCropModeCenter]; UIImage * cropped = [myImage cropToSize:(CGSize){width,height} usingMode:NYXCropModeCenter];

NYXCropMode is an enum type which can be found in the header file, it is used to represent the 9 modes above. NYXCropMode是一种枚举类型,可以在头文件中找到,它用于表示上面的9种模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM