0

虽然我将 imageView 设置为 aspectFit,但似乎通过 CIImage 创建的图像总是会被缩放以填充。

通过 2 种方式加载图像的简单示例:

// 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;

2 个图像视图(uiImageView 和 ciImageView)具有相同的“aspectFit”属性和相同的尺寸约束(200x200)。但只有(1)正确显示图像,(2)拉伸图像。

有什么问题?谢谢你。

我通过 Xcode 6.2 创建我的项目

在此处输入图像描述

4

2 回答 2

1

尝试通过 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();
于 2015-03-25T07:43:34.333 回答
0

CIImage 文档指出

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

UIImageView拉伸问题可能与将管理实际渲染的事实有关。从其他帖子和玩 CIFilters 来看,它可能使用视图矩形作为区域绘制或渲染 CIContext,而不是原始图像大小。

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

UIImage(cgImage: self.context.createCGImage(outputImage, from: outputImage.extent)!)
于 2019-03-20T21:46:17.143 回答