将UIImage转换为CIImage时的双图像大小

时间:2017-04-05 10:30:55

标签: ios objective-c iphone uiimage core-image

这是我的测试代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.uiImage = [self imageFromView:self.view];
    NSLog(@"Test1: uiImage.size = %@", NSStringFromCGSize(self.uiImage.size));

    self.ciImage = [[CIImage alloc] initWithCGImage:self.uiImage.CGImage];
    NSLog(@"Test1: ciImage.extent = %@", NSStringFromCGRect(self.ciImage.extent));

    self.uiImage = [UIImage imageNamed:@"test.jpg"];
    NSLog(@"Test2: uiImage.size = %@", NSStringFromCGSize(self.uiImage.size));

    self.ciImage = [[CIImage alloc] initWithCGImage:self.uiImage.CGImage];
    NSLog(@"Test2: ciImage.extent = %@", NSStringFromCGRect(self.ciImage.extent));

}

-(UIImage*)imageFromView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

我得到以下输出:

Test1: uiImage.size = {768, 1024}
Test1: ciImage.extent = {{0, 0}, {1536, 2048}}
Test2: uiImage.size = {400, 400}
Test2: ciImage.extent = {{0, 0}, {400, 400}}

我无法找到Test1中图像尺寸加倍的原因。我该如何解决?

1 个答案:

答案 0 :(得分:2)

UIImage大小以磅为单位。 CIImage范围以像素为单位。当您为第一次测试捕获UIView时,您在视网膜屏幕上这样做,因此1点== 2x2像素。当您从文件中加载第二张图片时,您需要加载非视网膜资源(test.jpg vs test@2x.jpg),因此1点== 1x1像素。

没有什么可以解决的 - 当您将视图捕获为图像时,它的大小不会加倍,而是您捕获的视图的大小(以像素为单位)。

如果您希望两个测试以相同的比例结束,那么请提供您的资产的视网膜版本。