iOS load dynamic layer mask (i.e. layer mask is provided outside the code)

喜你入骨 提交于 2019-12-08 03:45:33

问题


Situation: want to apply interesting photo frame to images, and the photo frame is implemented as layer mask, is it possible to dynamically build the layer mask by load a photo frame template outside of the obj-C code so that I can change the frame layer without ever touching the code?

the end result will be something like this. http://a3.mzstatic.com/us/r1000/106/Purple/9e/b9/9b/mzl.rdrrpcgr.320x480-75.jpg, except the photo edge/frame is dynamically loaded outside of the app, rather than built-into the app.

Ideally, would like to easily create a photo frame in photoshop as png file where the black pixel here will allow full transparency.. and then I can load this photo frame in the iOS app as the frame layer will allow the layer underneath to fully go through wherever the mask layer is black...


回答1:


+ (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

CGImageRef maskRef = maskImage.CGImage; 

CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), NULL, false);

CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];
}

Then you could use this method with a UIImage you load from a URL.

The URL could serve a different UIImage, or could take a parameter for which UIImageMask to load. Does this answer your question?



来源:https://stackoverflow.com/questions/9372858/ios-load-dynamic-layer-mask-i-e-layer-mask-is-provided-outside-the-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!