iphone - 在 iPhone 中裁剪具有透明度的图像

标签 iphone uiimage transparency

我正在开发拼图类型的游戏,其中我有两个用于 mask 的图像, 我已经实现了这段代码来进行屏蔽

- (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;
}

enter image description here

+

enter image description here

=

这是我屏蔽后得到的最终结果。

enter image description here

现在我想将图像裁剪成像 enter image description here 这样的片段和 enter image description here等等参数化(通过透明度裁剪图像)。

如果有人实现了此类代码或对此场景有任何想法,请分享。

谢谢。

我使用这行代码作为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++;
        }
    }

orgImg 是原始猫图像,frmImg 框架用于容纳单个片段,在 Photoshop 中进行 mask ,cropImg 是原始 cat@2x.png 的 106x106 裁剪图像。

我的裁剪函数如下

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

最佳答案

更新2

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

它包含:

  • 提供列/行数,它将生成具有正确宽度/高度的必要拼图。列/行越多 - 宽度/高度和轮廓/内嵌拼图形式越小。
  • 每次生成随机边
  • 可以在发射开始时随机定位/旋转棋子
  • 每个部件都可以通过点击或用两根手指(就像真正的部件一样)旋转 - 但一旦释放,它就会捕捉到 90/180/270/360 度
  • 如果触摸其“可触摸形状”边界(大部分是相同的可见拼图形状,但没有内联形状),则每个 block 都可以移动

缺点:

  • 不检查棋子是否在正确的位置
  • 如果超过 100 block - 它开始滞后,因为当拾取一 block 时,它会遍历所有 subview ,直到找到正确的 block 。

更新

感谢更新问题。

我设法得到了这个:

enter image description here

如您所见 - 拼图项目已正确裁剪,并且位于方形 imageView 中(绿色是 UIImageView 背景颜色)。

所以 - 我所做的是:

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.

对于图像屏蔽,我使用 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;
}

上一个答案

可以为每件准备一个面具吗?

例如,您有该帧图像。你能在 Photoshop 中将它剪切成 9 个单独的图像吗?在每个图像中它只会显示相应的部分。 (其余所有 - 删除)。

示例 - 第二 block mask :

enter image description here

然后,您在猫图像上使用这些新创建的蒙版图像中的每一个 - 每幅图像都会蒙版所有图像,但只有一个和平。因此,您将获得使用 9 个不同蒙版的 9 幅图像。

对于更大或不同的拼图框架 - 再次创建单独的图像蒙版。

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

希望有帮助..

关于iphone - 在 iPhone 中裁剪具有透明度的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10810798/

相关文章:

ios - 为什么我的 tableview 只按升序显示日期而不显示其余数据

ios - Swizzling UIImage init 不起作用 iOS Swift

iphone - 有没有办法在通用应用程序中共享(高分辨率)图形?

iphone - 如何在 Safari 中更改 iPhone 虚拟键盘上 "Go"按钮的文本?

ios - iPhone 上 Swift 中的图像缩放问题

ios - 将 Firebase 中的 imageURL 转换为 UIImage 的最快方法是什么?

java - 使用 java 将透明 gif/png 转换为 jpeg

java - 半透明文本框

javascript - 带有透明三 Angular 形的下拉菜单(需要帮助)

iphone - iOS 上的地理围栏