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

标签 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));

但是屏幕上显示的图像被拉伸(stretch)了。

我怎么了?或者我还有其他选择吗?

非常感谢,很抱歉我没有足够的声誉来张贴图片。 :)

最佳答案

试试这个:

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();

关于ios - 为什么图像拉伸(stretch)(UIImage 到 CIImage 到 UIImage),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29057398/

相关文章:

IOS 8 界面生成器 UITableViewCell 对齐

iphone - AVURLAsset 视频缓冲

objective-c - 找不到 MediaPlayer/Mediaplayer.h 文件

ios - 约束 uiimageview 以保持宽高比并根据设备大小进行扩展

iphone - 如何在 UITableView 单元格文本中进行自动收缩而不隐藏详细信息标签?

c++ - Cocos2dx InApp Purchase for ios

objective-c - 未调用 AQGridView didSelectItemAtIndex 委托(delegate)

ios - AWS Cognito Facebook 和 Google Plus 用户登录凭证

ios - 尝试在 iOS 中使用自定义名称将图像保存到相册

ios - 在 UIScrollView 中设置大图像