【发布时间】:2012-03-04 21:02:47
【问题描述】:
我想要实现的是使用 iPhone 相机拍照,然后用其他一些图片替换部分图片...我正在考虑拍照,然后用低 alpha 的 UIView 覆盖它,但随后我将无法仅替换图片的某些部分。如果你能给我一些想法,我会非常有礼貌。
【问题讨论】:
标签: ios uiview uiimage overlay
我想要实现的是使用 iPhone 相机拍照,然后用其他一些图片替换部分图片...我正在考虑拍照,然后用低 alpha 的 UIView 覆盖它,但随后我将无法仅替换图片的某些部分。如果你能给我一些想法,我会非常有礼貌。
【问题讨论】:
标签: ios uiview uiimage overlay
对于 iOS,有几个用于图像的 API,我理解您的困惑。
首先你有UIImage,图像的CocoaTouch 抽象。
然后你有CGImage,图像的CoreGraphics 抽象。
此外,您还有 CIImage,即图像的 CoreImage 抽象。
这些都是不同的实体,不能以一种很好的无桥方式一起使用。相反,您必须在不同的格式之间进行转换。
通常,您最终想要的是可以在小部件中显示的 UIImage。您可以从CGImage 或CIImage 创建此图像。 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();
【讨论】:
我在给你解释 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都是从UIImage转换而来的CIImage
现在使用此代码获取编辑后的 uiimage
CIImage *outputImage = [filter valueForKey:@"outputImage"];
CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *outputUIImage = [UIImage imageWithCGImage:cgImage];
【讨论】: