如何检测 UIImage 的非透明部分何时与 UIImage 的另一个非透明部分接触 [英] How to detect when the non-transparent part of an UIImage is in contact with another non-transparent part of an UIImage

查看:21
本文介绍了如何检测 UIImage 的非透明部分何时与 UIImage 的另一个非透明部分接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法完成我认为会容易得多的事情.每当 UIImage 中图片的非透明部分接触 UIImage 中包含的图像的另一个非透明部分时,我都会尝试运行一个方法.我提供了一个示例来帮助进一步解释我的问题.

I am having trouble accomplishing something that I thought was going to be much easier. I am trying to run a method whenever a non transparent part of a picture inside a UIImage touches another non-transparent part of an image contained within a UIImage. I have included an example to help further explain my question.

如上图所示,我有两个三角形,它们都在 UIImage 内.三角形都是PNG图片.只有三角形是可见的,因为背景已被设置为透明.两个 UIImages 都在 UIImageView 中.我希望能够在三角形的可见部分接触另一个三角形的可见部分时运行一个方法.有人可以帮我吗?

As you can see in the image above, I have two triangles that are both inside a UIImage. The triangles are both PNG pictures. Only the triangle is visible because the background has been made transparent. Both of the UIImages are inside a UIImageView. I want to be able to run a method when the visible part of the triangle touches the visible part of the other triangle. Can someone please help me?

推荐答案

我所做的是:

  • 计算图像 A 中非 alpha 像素的数量
  • 对图片 B 做了同样的事情
  • 将 A + B 图像合并为一张图像:C
  • 比较结果像素数

如果合并后像素数量较少,那么我们就成功了.

If pixes amount was less after merging then we have a hit.

if (C.count < A.count + B.count) -> 我们命中了

if (C.count < A.count + B.count) -> we have a hit

+ (int)countPoints:(UIImage *)img
{

    CGImageRef cgImage = img.CGImage;
    NSUInteger width = img.size.width;
    NSUInteger height = img.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    size_t bitsPerComponent = 8;
    size_t bytesPerPixel    = 1;
    size_t bytesPerRow      = width * bitsPerComponent * bytesPerPixel;
    size_t dataSize         = bytesPerRow * height;

    unsigned char *bitmapData = malloc(dataSize);
    memset(bitmapData, 0, dataSize);

    CGContextRef bitmap = CGBitmapContextCreate(bitmapData, width, height, bitsPerComponent, width, NULL,(CGBitmapInfo)kCGImageAlphaOnly);

    CGColorSpaceRelease(colorSpace);

    CGContextTranslateCTM(bitmap, 0, img.size.height);
    CGContextScaleCTM(bitmap, 1.0, -1.0);

    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), cgImage);

    int p = 0;
    int i = 0;
    while (i < width * height) {

        if (bitmapData[i] > 0) {
            p++;
        }
        i++;
    }

    free(bitmapData);
    bitmapData = NULL;
    CGContextRelease(bitmap);
    bitmap = NULL;

    //NSLog(@"points: %d",p);

    return p;
}
+ (UIImage *)marge:(UIImage *)imageA withImage:(UIImage *)imageB {

    CGSize itemSize = CGSizeMake(imageA.size.width,  imageB.size.width);
    UIGraphicsBeginImageContext(itemSize);

    CGRect rect = CGRectMake(0,
                             0,
                             itemSize.width,
                             itemSize.height);
    [imageA drawInRect:rect];
    [imageB drawInRect:rect];

    UIImage *overlappedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return overlappedImage;
}

这篇关于如何检测 UIImage 的非透明部分何时与 UIImage 的另一个非透明部分接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆