为什么图像拉伸(UIImage到CIImage到UIImage)

时间:2015-03-15 05:00:21

标签: ios objective-c uiimageview uiimage ciimage

由于某些原因,我需要将图像从UIImage转换为CIImage(用于处理)并再次转换回UIImage。我是通过UIImage-CGImage-CIImage-UIImage来做的。检查不相关的代码,代码显示在后面。

UIImage *originalPic = [[UIImage alloc]init];//get this from UIImagePickerController
CIImage *originalCIPic = [[CIImage alloc]init];
originalCIPic = [CIImage imageWithCGImage:originalPic.CGImage];
UIImage *finalResultUIImage = [UIImage imageWithCIImage:originalCIPic];

//then I put the image on an Image View(contentMode has been set)
self.viewOfImage.contentMode = UIViewContentModeScaleAspectFit; 
self.viewOfImage.image=finalResultUIImage;

//I used NSLog to check the size, but they are the same.(before transforming and after)
NSLog(@"this is the final UIImage,size is %@ ",NSStringFromCGSize(finalResultUIImage.size));

但是屏幕上显示的图像被拉长了。

我有什么问题?或者我有其他选择吗?

非常感谢,我很抱歉我没有足够的声望来发布图片。 :)

1 个答案:

答案 0 :(得分:1)

试试这个:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"abcd" ofType:@"png"];
CIImage *ciImage = [CIImage imageWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
UIImage *imageByCIImage = [UIImage imageWithCIImage:ciImage scale:1.0f orientation:UIImageOrientationUp];

UIGraphicsBeginImageContext(imageByCIImage.size);
[imageByCIImage drawInRect:CGRectMake(0,0,imageByCIImage.size.width,imageByCIImage.size.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageview.image = newImage;
UIGraphicsEndImageContext();