4

在我的应用程序中,我想执行以下步骤:

1 - 捕获屏幕,这部分对我来说没问题,我正在使用以下代码:

- (UIImage *)captureScreen {
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 0.0f);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

2 - 我用这个功能裁剪了图像

- (UIImage *)cropImage(UIImage *)image inRect:(CGRect)rect  {
    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
    UIImage *resultImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return resultImage;
}

3 - 然后我用纯黑白蒙版蒙版裁剪的图像

- (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 maskedRef = CGImageCreateWithMask([image CGImage], mask);
    UIImage *resultImage = [UIImage imageWithCGImage:maskedRef];
    CGImageRelease(mask);
    CGImageRelease(maskedRef);

    return resultImage;
}

但是,我得到的结果图像是在蒙版形状之外,图像是黑色而不是透明的。有谁能够帮助我?

4

2 回答 2

2

这对我有用。希望它也对你有用。

- (UIImage*) doImageMask:(UIImage *)mainImage:(UIImage*)maskImage{

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGImageRef maskImageRef = [maskImage CGImage];

// create a bitmap graphics context the size of the image
CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);


if (mainViewContentContext == NULL){
    return NULL;
}

CGFloat ratio = 0;

ratio = maskImage.size.width/ mainImage.size.width;

if(ratio * mainImage.size.height < maskImage.size.height) {
    ratio = maskImage.size.height/ mainImage.size.height;
} 

CGRect rect1  = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
CGRect rect2  = {{-((mainImage.size.width*ratio)-maskImage.size.width)/2 , -((mainImage.size.height*ratio)-maskImage.size.height)/2}, {mainImage.size.width*ratio, mainImage.size.height*ratio}};

CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
CGContextDrawImage(mainViewContentContext, rect2, mainImage.CGImage);

// Create CGImageRef of the main view bitmap content, and then
// release that bitmap context
CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);

UIImage *theImage = [UIImage imageWithCGImage:newImage];

CGImageRelease(newImage);

// return the image
return theImage;
}
于 2012-06-06T06:01:36.737 回答
2

我解决了我的问题,这是由于要屏蔽图像的 alpha 通道。所以在屏蔽之前,我创建了另一个带有 alpha 通道的 UIImage 并继续我的步骤。

这是使用 alpha 创建 UIImage 的代码

- (UIImage *)imageWithAlpha  {
    CGImageRef imageRef = self.CGImage;
    CGFloat width = CGImageGetWidth(imageRef);
    CGFloat height = CGImageGetHeight(imageRef);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context =  CGBitmapContextCreate(nil, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

    CGImageRef resultImageRef = CGBitmapContextCreateImage(context);
    UIImage *resultImage = [UIImage imageWithCGImage:resultImageRef scale:self.scale orientation:self.imageOrientation];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(resultImageRef);

    return resultImage;
}
于 2012-06-06T07:32:54.133 回答