【发布时间】:2011-01-31 14:43:34
【问题描述】:
我正在构建某种审查应用程序。到目前为止,我已经可以完成用我的 iPhone 拍摄的图像的像素化。
但我想最终实现这样的图像:http://images-mediawiki-sites.thefullwiki.org/11/4/8/8/8328511755287292.jpg
所以我的想法是对图像进行完全像素化,然后在其上添加蒙版,以达到预期的效果。因此,就图层而言,它就像: originalImage + maskedPixelatedVersionOfImage .. 我想在触摸图像时为蒙版设置动画,将蒙版缩放到所需的大小。您将手指放在图像上的时间越长,蒙版就越大...
经过一番搜索,我想这可以使用 CALayers 和 CAAnimation 来完成。但是,我如何将这些图层合成为可以保存在 iphone 相册中的图像?
我在这里采取了正确的方法吗?
编辑:
好的,我猜 Ole 的解决方案是正确的,虽然我仍然没有得到我想要的:我使用的代码是:
CALayer *maskLayer = [CALayer layer];
CALayer *mosaicLayer = [CALayer layer];
// Mask image ends with 0.15 opacity on both sides. Set the background color of the layer
// to the same value so the layer can extend the mask image.
mosaicLayer.contents = (id)[img CGImage];
mosaicLayer.frame = CGRectMake(0,0, img.size.width, img.size.height);
UIImage *maskImg = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mask" ofType:@"png"]];
maskLayer.contents = (id)[maskImg CGImage];
maskLayer.frame = CGRectMake(100,150, maskImg.size.width, maskImg.size.height);
mosaicLayer.mask = maskLayer;
[imageView.layer addSublayer:mosaicLayer];
UIGraphicsBeginImageContext(imageView.layer.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saver = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
所以在我的 imageView 中我做了:setImage,它具有照片的原始(未编辑)版本。最重要的是我添加了一个子层,mosaicLayer,它有一个遮罩属性:maskLayer。我想通过渲染 imageView 的根层,一切都会好起来的。这不正确吗?
另外,我还发现了一些别的东西:我的面具被拉伸和旋转了,我猜这与 imageOrientation 有关吗?我注意到不小心将mosaicLayer 保存到我的库中,这也解释了我遇到的问题,即蒙版似乎掩盖了我图像的错误部分......
【问题讨论】:
标签: iphone objective-c core-graphics core-animation quartz-graphics