3

好吧,我想做的是:

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

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

4

1 回答 1

5

不幸的是,您不能使用CoreAnimation来执行此操作(这会很容易)。查看 Apple 的 CoreAnimation文档

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

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

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;
于 2010-09-03T04:18:32.053 回答