【问题标题】:Creating mask with CGImageMaskCreate is all black (iphone)用 CGImageMaskCreate 创建蒙版是全黑的(iphone)
【发布时间】:2010-10-12 14:16:08
【问题描述】:

我正在尝试从两个现有图像的合成中创建一个图像蒙版。

首先,我开始创建一个合成图像,其中包含一个作为掩蔽图像的小图像和一个与背景大小相同的较大图像:

UIImage *baseTextureImage = [UIImage imageNamed:@"background.png"];
UIImage *maskImage = [UIImage imageNamed:@"my_mask.jpg"];
UIImage *shapesBase = [UIImage imageNamed:@"largerimage.jpg"];
UIImage *maskImageFull;

CGSize finalSize = CGSizeMake(480.0, 320.0);
UIGraphicsBeginImageContext(finalSize);
[shapesBase drawInRect:CGRectMake(0, 0, 480, 320)];
[maskImage drawInRect:CGRectMake(150, 50, 250, 250)];
maskImageFull = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我可以输出这个 UIImage (MaskImageFull) 并且看起来不错。这是一个全尺寸的背景尺寸,它有一个白色背景,我的蒙版对象是黑色的,在屏幕上的正确位置。

然后我通过这个传递 MaskImageFull UIImage:

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);
UIImage *retImage= [UIImage imageWithCGImage:masked];

问题是 retImage 是全黑的。如果我发送一个预制的 UIImage 作为掩码,它可以正常工作,只是当我尝试从多个图像中制作它时它会中断。

我认为这是一个色彩空间问题,但似乎无法修复它。非常感谢任何帮助!

【问题讨论】:

    标签: objective-c iphone cocoa-touch core-graphics image-manipulation


    【解决方案1】:

    我用 CGImageCreateWithMask 尝试了同样的事情,得到了同样的结果。我找到的解决方案是改用 CGContextClipToMask:

    CGContextRef mainViewContentContext;
    CGColorSpaceRef colorSpace;
    
    colorSpace = CGColorSpaceCreateDeviceRGB();
    
    // create a bitmap graphics context the size of the image
    mainViewContentContext = CGBitmapContextCreate (NULL, targetSize.width, targetSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
    
    // free the rgb colorspace
    CGColorSpaceRelease(colorSpace);    
    
    if (mainViewContentContext==NULL)
        return NULL;
    
    CGImageRef maskImage = [[UIImage imageNamed:@"mask.png"] CGImage];
    CGContextClipToMask(mainViewContentContext, CGRectMake(0, 0, targetSize.width, targetSize.height), maskImage);
    CGContextDrawImage(mainViewContentContext, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), self.CGImage);
    
    
    // Create CGImageRef of the main view bitmap content, and then
    // release that bitmap context
    CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);
    CGContextRelease(mainViewContentContext);
    
    // convert the finished resized image to a UIImage 
    UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
    // image is retained by the property setting above, so we can 
    // release the original
    CGImageRelease(mainViewContentBitmapContext);
    
    // return the image
    return theImage;
    

    【讨论】:

    • 帮了我很大的忙。能不能把函数的页眉和页脚加个完整的?
    • thumbnailPoint.x 到底是什么??! @catlan
    • @Reza.Ab 抱歉,我不记得了。 scaled* 在 9 年后看起来也很奇怪。我假设在大多数情况下,掩码矩形和图像矩形应该是相同的。请尝试一下,让我们知道您发现了什么,
    【解决方案2】:

    要屏蔽的图像必须使用 Alpha 通道创建。 Alpha 通道可能不是通过代码创建的。

    【讨论】:

      【解决方案3】:
      - (UIImage *) maskImage:(UIImage *)image {
          
          CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
          
          UIImage *maskImage = [UIImage imageNamed:@"MaskFinal.png"];
          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/ image.size.width;
          
          if(ratio * image.size.height < maskImage.size.height) {
              ratio = maskImage.size.height/ image.size.height;
          } 
          
          CGRect rect1 = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
          CGRect rect2 = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};
          
          
          CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
          CGContextDrawImage(mainViewContentContext, rect2, image.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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-13
        • 2023-03-28
        • 2013-07-20
        • 2019-10-24
        • 2019-07-23
        • 1970-01-01
        相关资源
        最近更新 更多