如何使CGImageCreateWithImageInRect引用转换后的图像?

时间:2014-06-02 17:03:20

标签: ios objective-c graphics

Xcode 5,iOS 7

我正在将图像加载到UIImage中,然后将其复制到另一个带镜像转换的UIImage中,即:

self.imageB.image=[UIImage imageWithCGImage:[self.imageA.image CGImage] scale:1.0 orientation:UIImageOrientationUpMirrored];

接下来,我试图在保存时将两个图像合并为一个(originalImage指的是在复制/转换为imageA和imageB之前加载的图像):

newSize=CGSizeMake(originalImage.size.width*2,originalImage.size.height);
UIGraphicsBeginImageContext(newSize);
UIImage *leftImage=self.imageA.image;
UIImage *rightImage=self.imageB.image;
[self.imageA.image drawInRect:CGRectMake(0,0,newSize.width/2,newSize.height)];
[self.imageB.image drawInRect:CGRectMake(newSize.width/2,0,newSize.width/2,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);

这可以工作,并为我提供一个保存的镜像图像。 但是,我试图仅使用mageA和imageB的子区域来做到这一点, 结果是来自imageB的部分失去了它的镜像转换。 我最终得到了两个方面相同的方向!(注意:visRatio是我要保留的图像的百分比)

newSize=CGSizeMake(originalImage.size.width*2,originalImage.size.height);
UIGraphicsBeginImageContext(newSize);
UIImage *leftImage=self.imageA.image;
UIImage *rightImage=self.imageB.image;

CGRect clippedRectA  = CGRectMake(0,0,originalImage.size.width*visRatio,originalImage.size.height);
CGImageRef imageRefA = CGImageCreateWithImageInRect([self.imageA.image CGImage], clippedRectA);
UIImage *leftImageA   = [UIImage imageWithCGImage:imageRefA];
[leftImageA drawInRect:CGRectMake(0,0,newSize.width/2,newSize.height)];

CGRect clippedRectB  = CGRectMake(originalImage.size.width-(originalImage.size.width*visRatio),0,originalImage.size.width*visRatio,originalImage.size.height);
CGImageRef imageRefB = CGImageCreateWithImageInRect([self.imageB.image CGImage], clippedRectB);
UIImage *rightImageB   = [UIImage imageWithCGImage:imageRefB];
[rightImageB drawInRect:CGRectMake(newSize.width/2,0,newSize.width/2,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);

就像“CGImageCreateWithImageInRect”从原始数据而不是转换后的数据中复制图像一样。 如何在不丢失imageB的镜像转换的情况下完成此操作?

1 个答案:

答案 0 :(得分:0)

我想通了 - 这将使用打开到originalImage的原始全分辨率数据并保存一个剪切的转换版本(为了简单起见,我将镜像留在了这篇文章中):

    //capture the transformed version
    CGSize tmpSize=CGSizeMake(originalImage.size.width, originalImage.size.height);
    UIGraphicsBeginImageContext(tmpSize);
    [self.imageA.image drawInRect:CGRectMake(0,0, tmpSize.width, tmpSize.height)];
    UIImage *tmpImageA=UIGraphicsGetImageFromCurrentImageContext();
    visRatio=0.5;
    //clip it
    clippedRectA=CGRectMake(0,0,roundf(originalImage.size.width*visRatio),originalImage.size.height);
    imageRefA=CGImageCreateWithImageInRect([tmpImageA CGImage],clippedRectA);
    leftImageA=[UIImage imageWithCGImage:imageRefA];
    UIGraphicsEndImageContext();

    savedImage=UIGraphicsGetImageFromCurrentImageContext();
    UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil);

我认为关键是将图像绘制到上下文中,然后使用上下文进行裁剪/保存,而不是使用主图像(imageA)。 如果您找到更有效的方式,请发布! 否则,我希望这有助于其他人......