繁体   English   中英

iOS CIImage拉伸图像

[英]iOS CIImage stretch the image

尽管我将imageView设置为AspectFit,但似乎通过CIImage创建的图像将始终被缩放以填充。

一个通过两种方式加载图像的简单示例:

// 1. load directly via UIImage
UIImage *imageByUIImage = [UIImage imageNamed:@"Megan-Fox-Look.jpg"];
self.uiImageView.image = imageByUIImage;

// 2. load via CIImage
NSString *filePath =
[[NSBundle mainBundle] pathForResource:@"Megan-Fox-Look" ofType:@"jpg"];
CIImage *ciImage =
[CIImage imageWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
UIImage *imageByCIImage = [UIImage imageWithCIImage:ciImage scale:1.f orientation:UIImageOrientationUp];
self.ciImageView.image = imageByCIImage;

这两个图像视图(uiImageView和ciImageView)具有相同的'aspectFit'属性和相同的尺寸限制(200x200)。 但是只有(1)正确显示图像,(2)拉伸图像。

有什么问题? 谢谢。

我用Xcode 6.2创建我的项目

在此处输入图片说明

尝试通过CIImage加载:

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

CIImage文档指出

尽管CIImage对象具有与之关联的图像数据,但它不是图像

拉伸问题可能与UIImageView将管理实际渲染这一事实有关。 在其他文章中,以及使用CIFilters的过程中,它可能使用CIContext使用rect视图作为区域而不是原始图像大小来进行绘制或渲染。

一篇文章解决了这个问题,Matt的这篇文章还提到了其他文章中看到的一个选项:使用CGImageRef中介表示。

UIImage(cgImage: self.context.createCGImage(outputImage, from: outputImage.extent)!)

暂无
暂无

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

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