ios*_*per 5 iphone objective-c uiimage
我想掩盖两个图像
图1:

图2:

现在我想合并这两个图像像image2将来到image1的中心.
我读过任何想法为什么这个图像屏蔽代码不起作用?和"如何掩盖图像"
但是在使用这个功能时: -
- (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 masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];
}
Run Code Online (Sandbox Code Playgroud)
但通过使用此功能,我得到的输出是: -

据我所知问题不在于maskImage:withMask:. 您发布的错误输出并没有错误:image1 的像素为黑色,image2 不可见。
image问题可能出在您用来加载和 的函数中mask,或者出在生成图形输出的代码中。事实上,在我看来,你得到了正确的输出,但使用了错误的色彩空间(没有 alpha 的灰度)。检查您提供的参数是否image实际上是 RGBa 格式,以及返回的参数是否UIImage未使用 DeviceGray 作为颜色空间绘制到其他参数上CGContext。
我想到的另一个可能的原因是你颠倒了 image1 和 image2。根据文档,mask应该放大到image(见下文)的大小,但那里有一个小图像。这似乎也是合理的,因为 image1 是灰度的,尽管当我尝试交换 image1 和 image2 时,我得到的 image1 未修改为输出。
为了运行测试,我使用了附加的图像,然后按原样复制并粘贴该方法-(UIImage *)maskImage:(UIImage *)image withMask:(UIImage *)maskImage。
我使用了一个简单的 iOS 项目,其中有一个视图和一个UIImageView内部(带有标签 1),控制器中包含以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imageView=(UIImageView *)[self.view viewWithTag:1];
UIImage *im1=[UIImage imageNamed:@"image1"];
UIImage *im2=[UIImage imageNamed:@"image2"];
imageView.image=[self maskImage:im2 withMask:im1];
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出(这是正确的):

如果错误地将 im1 与 im2 反转,则会得到未修改的 image1。
| 归档时间: |
|
| 查看次数: |
1123 次 |
| 最近记录: |