您的位置:首页 > 移动开发 > IOS开发

iOS图片裁剪为正方形时imageOrientation自动改变了

2016-05-05 15:21 465 查看
拍照或者相册的图片上传.

要求上传正方形,在裁剪的时候发现imageOrientation自动改变了.

但是从相册选不会,拍照会.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YEScompletion:^{}];
UIImage *image = [infoobjectForKey:UIImagePickerControllerOriginalImage];
//图片裁剪成正方形
UIImage *resizeImage;
CGFloat iw = image.size.width;
CGFloat ih = image.size.height;
CLog(@"image's size w:%f h:%f",iw,ih);
if (iw > ih) {//长大于宽
CGImageRef cgimage =CGImageCreateWithImageInRect([imageCGImage], CGRectMake(0,0, ih, ih));
resizeImage = [[UIImagealloc] initWithCGImage:cgimagescale:1orientation:image.imageOrientation];
} else {
CGImageRef cgimage =CGImageCreateWithImageInRect([imageCGImage], CGRectMake(0,0, iw, iw));
resizeImage = [[UIImagealloc] initWithCGImage:cgimagescale:1orientation:image.imageOrientation];
}
}

之前用的是 

  resizeImage = [UIImageimageWithCGImage:cgimage];
改成 resizeImage = [[UIImage alloc] initWithCGImage:cgimage scale:1 orientation:image.imageOrientation];就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息