Masking an Image with an Image?

Dont work with code:

    CGImageRef localResultImage;
    CGImageRef localMaskImage;

    size_t width = CGImageGetWidth(myImageMask);
    size_t height = CGImageGetHeight(myImageMask);
    size_t bitsPerComponent = CGImageGetBitsPerComponent(myImageMask);
    size_t bitsPerPixel = CGImageGetBitsPerPixel(myImageMask);
    size_t bytesPerRow = CGImageGetBytesPerRow(myImageMask);
    CGDataProviderRef provider = CGImageGetDataProvider(myImageMask);
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(myImageMask);
    CGColorSpaceRef space = CGImageGetColorSpace(myImageMask);
    CGColorRenderingIntent intent = CGImageGetRenderingIntent(myImageMask);

    localMaskImage = CGImageCreate(width,
                                   height,
                                   bitsPerComponent,
                                   bitsPerPixel,
                                   bytesPerRow,
                                   space,
                                   bitmapInfo,
                                   provider,
                                   NULL,
                                   TRUE,
                                   intent);

    localResultImage = CGImageCreateWithMask(myImage, localMaskImage);

    CGContextDrawImage(myContext, dirtyRect, localResultImage);

localResultImage = (null) - Why?

In documentation Apple: Instead of passing an image mask that's created using the function CGImageMaskCreate, you supply an image created from one of the Quartz image-creation functions.

Source samples of an image that is used as a mask (but is not a Quartz image mask) operate as alpha values. .... In this case, assume that the image shown in Figure 11-6 is created using one of the Quartz image-creation functions, such as CGImageCreate.

but does not work...Hmm

log Info myImageMask:
Width: 440
Height: 292
BitsPerComponent: 8
BitsPerPixel: 8
BytesPerRow: 440
BitmapInfo: 0
ColorSpace: (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Dot Gain 20%)
RenderingIntent: 2


The only thing which can cause the problem is to have myImage as null in the following line. Rest all code looks good.

localResultImage = CGImageCreateWithMask(myImage, localMaskImage);

If myImage is an object of class UIImage then you should use below line:

localResultImage = CGImageCreateWithMask([myImage CGImage], localMaskImage);
链接地址: http://www.djcxy.com/p/46528.html

上一篇: 如何确定CGImage的每个像素的组件数量?

下一篇: 用图像掩盖图像?