2012-03-04 143 views
2

我想要实现的是拍摄一张使用iPhone相机的图片,然后用其他一些图片替换部分图片...我正在考虑拍摄该图片,然后用UIView叠加低阿尔法,但我不能只取代图片的某些部分。如果你能给我一些想法,我会非常成功。更改图片背景图片

回答

1

对于iOS,有几种图像API,我理解你的困惑。 首先你有UIImage,CocoaTouch图像抽象。 然后你有CGImage,图像的CoreGraphics抽象。 此外,您还有CIImage,图像的CoreImage抽象。

这些都是不同的实体,不能以一个很好的无桥方式一起使用。相反,你必须转换成不同的格式。

通常,最终你想要的是一个UIImage,你可以在一个小部件中显示。您可以从CGImageCIImage创建此图像。 CIImage包含高级过滤器功能,例如,您可以使用适当的合成过滤器描述其他图像。

CIImage以任何方式更快更好,但它在iOS中仍然不完全支持。您可能还没有为iOS创建自己的自定义过滤器,只有一小部分过滤器尚未得到支持。

因此,我会建议使用CGImage为此目的。您需要渲染到新的渲染上下文,然后从该渲染上下文创建UIImage。

这应该做的伎俩:

UIImage *originalImage = ...; 
UIImage *frontImage = ...; 
CGSize destinationSize = originalImage.frame.size; 

UIGraphicsBeginImageContext(destinationSize); 
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)]; 
[originalImage drawInRect:CGRectMake(10,10,destinationSize.width - 20,destinationSize.height - 20)]; 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
0

我解释你在点 1.使用quartzcore库 2.转换这两个图像ciimage。转换 代码是:(有很多,我选址许多之一)

CIImage *paper1 =[CIImage imageWithData:UIImageJPEGRepresentation([UIImage imageNamed:@"heart123.png"], 1)]; 

现在,根据您的需要, 如..“CISourceAtopCompositing”或“CISourceOverCompositing”过滤器 ,你会得到使用过滤器你想

代码使用它们的是什么:

CIContext *context = [CIContext contextWithOptions:nil]; 

    CIFilter filter= [CIFilter filterWithName:@"CISourceAtopCompositing"]; //for CISourceAtopCompositing method 
    [filter setDefaults]; 
    [filter setValue:sourceImage forKey:@"inputImage"]; 
    [filter setValue:backgroundImage forKey:@"inputBackgroundImage"]; 

这里既有和backgroundImage和sourceImage是CIImage从UIImage的

转换

我们得到的UIImage编辑使用这个代码后

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