在iPhone中裁剪图像的透明度

iCo*_*r86 22 iphone transparency uiimage

我正在研究Jigsaw类型的游戏,我有两个用于屏蔽的图像,我已经实现了这个代码用于屏蔽

- (UIImage*) maskImage:(UIImage *)image withMaskImage:(UIImage*)maskImage {

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGImageRef maskImageRef = [maskImage CGImage];

    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);

    CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
    CGContextRelease(mainViewContentContext);

    UIImage *theImage = [UIImage imageWithCGImage:newImage];
    CGImageRelease(newImage);
    return theImage;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

+

在此输入图像描述

=

这是我掩盖后的最终结果.

在此输入图像描述

现在我想像片中裁剪图像 在此输入图像描述在此输入图像描述 等参数化(通过透明度裁剪图像).

如果有人在这个场景中实现了这样的代码或任何想法,请分享.

谢谢.

我正在使用这行代码作为Guntis Treulands的建议

int i=1;
    for (int x=0; x<=212; x+=106) {
        for (int y=0; y<318; y+=106) {
            CGRect rect = CGRectMake(x, y, 106, 106);
            CGRect rect2x = CGRectMake(x*2, y*2, 212, 212);

            UIImage *orgImg = [UIImage imageNamed:@"cat@2x.png"];
            UIImage *frmImg = [UIImage imageNamed:[NSString stringWithFormat:@"%d@2x.png",i]];
            UIImage *cropImg = [self cropImage:orgImg withRect:rect2x];

            UIImageView *tmpImg = [[UIImageView alloc] initWithFrame:rect];
            [tmpImg setUserInteractionEnabled:YES];
            [tmpImg setImage:[self maskImage:cropImg withMaskImage:frmImg]];

            [self.view addSubview:tmpImg];
            i++;
        }
    }
Run Code Online (Sandbox Code Playgroud)

orgImg是原始猫图像,用于保存单个片段的frmImg框架,在photoshop中屏蔽并且cropImg是原始cat@2x.png的106x106裁剪图像.

我的裁剪功能如下

- (UIImage *) cropImage:(UIImage*)originalImage withRect:(CGRect)rect { 
    return [UIImage imageWithCGImage:CGImageCreateWithImageInRect([originalImage CGImage], rect)]; 
}
Run Code Online (Sandbox Code Playgroud)

Gun*_*nds 17

更新2

我很想找到一个更好的方法来创建一个拼图游戏,所以我花了两个周末创建了一个拼图游戏的演示项目.

它包含:

  • 提供列/行计数,它将生成具有正确宽度/高度的必要拼图.列/行越多 - 宽度/高度和轮廓/内联拼图形式越小.
  • 每次都产生随机的边
  • 可以在发射开始时随机定位/旋转棋子
  • 每件都可以通过水龙头或两根手指(如真品)旋转 - 但一旦释放,它会突然移动到90/180/270/360度
  • 如果触摸它的"可触摸形状"边界(这大部分是 - 相同的可见拼图形状,但没有内嵌形状),每个部分都可以移动

缺点:

  • 不检查是否在正确的位置
  • 如果超过100件 - 它开始滞后,因为,当拿起一块时,它会遍历所有子视图,直到找到正确的部分.

UPDATE

感谢更新的问题.

我设法得到了这个:

在此输入图像描述

正如您所看到的 - 拼图项目被正确裁剪,并且它是方形imageView (绿色是UIImageView backgroundColor).

所以 - 我做的是:

CGRect rect = CGRectMake(105, 0, 170, 170); //~ location on cat image where second Jigsaw item will be.

UIImage *originalCatImage = [UIImage imageNamed:@"cat.png"];//original cat image

UIImage *jigSawItemMask = [UIImage imageNamed:@"JigsawItemNo2.png"];//second jigsaw item mask (visible in my answer) (same width/height as cat image.)

UIImage *fullJigSawItemImage = [jigSawItemMask maskImage:originalCatImage];//masking - so that from full cat image would be visible second jigsaw item

UIImage *croppedJigSawItemImage = [self fullJigSawItemImage withRect:rect];//cropping so that we would get small image with jigsaw item centered in it.
Run Code Online (Sandbox Code Playgroud)

对于图像蒙版,我使用UIImage类别功能:(但你可以使用你的掩蔽功能.但我会发布它.)

- (UIImage*) maskImage:(UIImage *)image  
{     
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

     UIImage *maskImage = self;
     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;
}
Run Code Online (Sandbox Code Playgroud)

以前的答案

你能为每件作品准备一个面具吗?

例如,您有该帧图像.你可以在9个单独的图像中在Photoshop中剪切它,在每个图像中它只显示相应的片段.(所有其余的 - 删除).

示例 - 第二件面具:

在此输入图像描述

然后你在猫图像上使用这些新创建的面具图像中的每一个 - 每个部分将掩盖所有图像,但是一个和平.因此,您将使用9种不同的面具拍摄9张图像.

对于更大或不同的拼图框架 - 再次创建分离的图像蒙版.

这是一个基本的解决方案,但并不完美,因为你需要单独准备每个和平面具.

希望能帮助到你..