添加贴纸时来自CgImageRef的UIImage

时间:2013-03-12 01:00:32

标签: ios uiimage core-image ciimage

UIImage *sticky = [UIImage imageNamed:@"Radio.png"];
[_imgViewSticky setImage:sticky];
CIImage *outputImage = [self.originalImage CIImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImg = [context createCGImage:outputImage fromRect:[outputImage extent]];
float widthRatio = [outputImage extent].size.width / 320;
float heighRatio = [outputImage extent].size.height / 480;
CGPoint cgStickyPoint = CGPointMake(_imgViewSticky.frame.origin.x * widthRatio, _imgViewSticky.frame.origin.y * heighRatio);

cgImg = [self setStickyForCGImage:cgImg withPosition:cgStickyPoint];

最后一行返回一个CGImageRef对象。

我正在将值分配给最终图像:

UIImage *finalImage = [UIImage ImageWithCGImageRef:cgImg];

然而我没有得到图像。有什么想法吗?任何帮助深表感谢。

1 个答案:

答案 0 :(得分:3)

我注意到你的CIContext没有收到任何图纸,这可能是你没有得到图像的原因。我没有你想要的清晰图片,但是这段代码会将一个UIImage叠加在另一个UIImage之上:

UIGraphicsBeginImageContextWithOptions(backgroundImage.size, NO, 0.0); //Create an image context
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)]; //Draw the first UIImage
[stickerImage drawInRect:stickerRect]; //Draw the second UIImage wherever you want on top of the first image
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); //Get the final UIImage result