【问题标题】:mask image via another image通过另一个图像遮罩图像
【发布时间】:2011-04-07 16:10:16
【问题描述】:

好吧,我想做的是:

  • 给定一个图像,其中该图像中有一个“空白”圆圈。我想从用户库中获取现有图像,然后对其进行遮罩,以便该图像的特定部分仅显示在“空白”图像上。.

我尝试了一些屏蔽代码,但它们似乎都以相反的方式工作......关于如何解决这个问题的任何提示?

【问题讨论】:

    标签: iphone image uiimage masking


    【解决方案1】:

    很遗憾,您不能使用 CoreAnimation 来执行此操作(这会很容易)。 看苹果的CoreAnimationdocumentation

    iOS 注意:出于性能考虑,iOS 不支持 mask 属性。

    因此,下一个最好的方法是使用Quartz 2D(如回答here):

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 2022-10-23
      • 2013-01-11
      • 1970-01-01
      • 2018-05-29
      • 2016-06-09
      • 2016-01-07
      相关资源
      最近更新 更多