Retina Display上的核心图形=错误的png大小

时间:2014-03-18 17:03:10

标签: ios xcode core-graphics

我有一个iOS应用程序,使用相机拍摄照片,将其裁剪成方形,然后在顶部覆盖png。虽然它有效,但@ 2x视网膜图像并不尊重它的640x640原始大小,并以1280x1280(视网膜大小)显示

这意味着原始照片和叠加层不排成一行。

任何帮助?

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerEditedImage];
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];

UIImage *borderImage = [UIImage imageNamed:[NSString stringWithFormat:@"Overlay4.png"]];

NSData *dataFromImage = UIImageJPEGRepresentation(imageView.image, 1);


CIImage *beginImage= [CIImage imageWithData:dataFromImage];

CIContext *context = [CIContext contextWithOptions:nil];
CIImage *border =[CIImage imageWithData:UIImagePNGRepresentation(borderImage)];

CIFilter *filter= [CIFilter filterWithName:@"CISourceOverCompositing"];  
[filter setDefaults];
[filter setValue:border forKey:@"inputImage"];

[filter setValue:beginImage forKey:@"inputBackgroundImage"];

CIImage *outputImage = [filter valueForKey:@"outputImage"];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];

imageView.image = newImg;

}

2 个答案:

答案 0 :(得分:0)

假设您使用的是xcode 5+,请尝试查看media.xcassets文件夹并查看放置“overlay4.png”的位置(在1x或2x框中)。

它可能就像移动它一样简单。

答案 1 :(得分:0)

不确定这是否能真正解决您的问题,但它取代了UIImageXXXRepresentation和imageWithData的可疑用法:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    image = [info objectForKey:UIImagePickerControllerEditedImage];
    [imageView setImage:image];
    [self dismissViewControllerAnimated:YES completion:NULL];

    UIImage *borderImage = [UIImage imageNamed:[NSString stringWithFormat:@"Overlay4.png"]];
    CIImage *border = [borderImage CIImage];

    CIImage *beginImage= [image CIImage];

    CIContext *context = [CIContext contextWithOptions:nil];

    CIFilter *filter= [CIFilter filterWithName:@"CISourceOverCompositing"];
    [filter setDefaults];
    [filter setValue:border forKey:@"inputImage"];
    [filter setValue:beginImage forKey:@"inputBackgroundImage"];

    CIImage *outputImage = [filter valueForKey:@"outputImage"];
    UIImage *newImg = [UIImage imageWithCIImage:outputImage];

    imageView.image = newImg;
}

这可能允许维持比例参数并使事情按预期工作。最终的答案可能是首先不使用@ 2x图像,而是用低分辨率图像替换它,因为无论如何所有合成都要维护。或者,您可以在合成之前将@ 2x图像缩放为640x640(或照片为1280x1280)。