如何检查 uiimage 是否为空? (空、透明)

发布于 2024-10-13 03:22:19 字数 105 浏览 1 评论 0原文

检查 UIImage 是否为空的最佳方法是什么?
我有一个返回 UIImage 的绘画编辑器;如果这张图片上没有任何内容,我不想保存它。

which is the best way to check whether a UIImage is blank?
I have this painting editor which returns a UIImage; I don't want to save this image if there's nothing on it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心病无药医 2024-10-20 03:22:19

尝试以下代码:

BOOL isImageFlag=[self checkIfImage:image];

和 checkIfImage 方法:

- (BOOL) checkIfImage:(UIImage *)someImage {
    CGImageRef image = someImage.CGImage;
    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);
    GLubyte * imageData = malloc(width * height * 4);
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * width;
    int bitsPerComponent = 8;
    CGContextRef imageContext =
    CGBitmapContextCreate(
                          imageData, width, height, bitsPerComponent, bytesPerRow, CGImageGetColorSpace(image),
                          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
                          );

    CGContextSetBlendMode(imageContext, kCGBlendModeCopy);
    CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);
    CGContextRelease(imageContext);

    int byteIndex = 0;

    BOOL imageExist = NO;
    for ( ; byteIndex < width*height*4; byteIndex += 4) {
        CGFloat red = ((GLubyte *)imageData)[byteIndex]/255.0f;
        CGFloat green = ((GLubyte *)imageData)[byteIndex + 1]/255.0f;
        CGFloat blue = ((GLubyte *)imageData)[byteIndex + 2]/255.0f;
        CGFloat alpha = ((GLubyte *)imageData)[byteIndex + 3]/255.0f;
        if( red != 1 || green != 1 || blue != 1 || alpha != 1 ){
            imageExist = YES;
            break;
        }
    }

    free(imageData);        
    return imageExist;
}

您必须添加 OpenGLES 框架并将其导入到 .m 文件中:

#import <OpenGLES/ES1/gl.h>

Try this code:

BOOL isImageFlag=[self checkIfImage:image];

And checkIfImage method:

- (BOOL) checkIfImage:(UIImage *)someImage {
    CGImageRef image = someImage.CGImage;
    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);
    GLubyte * imageData = malloc(width * height * 4);
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * width;
    int bitsPerComponent = 8;
    CGContextRef imageContext =
    CGBitmapContextCreate(
                          imageData, width, height, bitsPerComponent, bytesPerRow, CGImageGetColorSpace(image),
                          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
                          );

    CGContextSetBlendMode(imageContext, kCGBlendModeCopy);
    CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);
    CGContextRelease(imageContext);

    int byteIndex = 0;

    BOOL imageExist = NO;
    for ( ; byteIndex < width*height*4; byteIndex += 4) {
        CGFloat red = ((GLubyte *)imageData)[byteIndex]/255.0f;
        CGFloat green = ((GLubyte *)imageData)[byteIndex + 1]/255.0f;
        CGFloat blue = ((GLubyte *)imageData)[byteIndex + 2]/255.0f;
        CGFloat alpha = ((GLubyte *)imageData)[byteIndex + 3]/255.0f;
        if( red != 1 || green != 1 || blue != 1 || alpha != 1 ){
            imageExist = YES;
            break;
        }
    }

    free(imageData);        
    return imageExist;
}

You will have to add OpenGLES framework and import this in the .m file:

#import <OpenGLES/ES1/gl.h>
高冷爸爸 2024-10-20 03:22:19

一种想法是调用 UIImagePNGRepresentation 获取 NSData 对象,然后将其与预定义的“空”版本进行比较 - 即:调用:

- (BOOL)isEqualToData:(NSData *)otherData

测试?

没有在大数据上尝试过这个;如果您的图像数据很大,您可能想要检查性能,否则如果它很小,可能就像在 C 中调用 memcmp() 一样。

One idea would be to call UIImagePNGRepresentation to get an NSData object then compare it with a pre-defined 'empty' version - ie: call:

- (BOOL)isEqualToData:(NSData *)otherData

to test?

Not tried this on large data; might want to check performance, if your image data is quite large, otherwise if it's small it is probably just like calling memcmp() in C.

感悟人生的甜 2024-10-20 03:22:19

沿着这些思路:

  1. 创建一个 1 像素的正方形 CGContext
  2. 绘制图像,使其填充上下文
  3. 测试上下文的一个像素以查看它是否包含任何数据。如果它是完全透明的,请将图片视为空白

其他人也许可以为此答案添加更多详细信息。

Something along these lines:

  1. Create a 1 px square CGContext
  2. Draw the image so it fills the context
  3. Test the one pixel of the context to see if it contains any data. If it's completely transparent, consider the picture blank

Others may be able to add more details to this answer.

流心雨 2024-10-20 03:22:19

这是一个 Swift 解决方案,不需要任何额外的框架。

感谢此处相关问题的回答:
从坐标获取 ImageView 的像素数据xcode 上的触摸屏?

func imageIsEmpty(_ image: UIImage) -> Bool {
    guard let cgImage = image.cgImage,
          let dataProvider = cgImage.dataProvider else
    {
        return true
    }

    let pixelData = dataProvider.data
    let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
    let imageWidth = Int(image.size.width)
    let imageHeight = Int(image.size.height)
    for x in 0..<imageWidth {
        for y in 0..<imageHeight {
            let pixelIndex = ((imageWidth * y) + x) * 4
            let r = data[pixelIndex]
            let g = data[pixelIndex + 1]
            let b = data[pixelIndex + 2]
            let a = data[pixelIndex + 3]
            if a != 0 {
                if r != 0 || g != 0 || b != 0 {
                    return false
                }
            }
        }
    }

    return true
}

Here's a solution in Swift that does not require any additional frameworks.

Thanks to answers in a related question here:
Get Pixel Data of ImageView from coordinates of touch screen on xcode?

func imageIsEmpty(_ image: UIImage) -> Bool {
    guard let cgImage = image.cgImage,
          let dataProvider = cgImage.dataProvider else
    {
        return true
    }

    let pixelData = dataProvider.data
    let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
    let imageWidth = Int(image.size.width)
    let imageHeight = Int(image.size.height)
    for x in 0..<imageWidth {
        for y in 0..<imageHeight {
            let pixelIndex = ((imageWidth * y) + x) * 4
            let r = data[pixelIndex]
            let g = data[pixelIndex + 1]
            let b = data[pixelIndex + 2]
            let a = data[pixelIndex + 3]
            if a != 0 {
                if r != 0 || g != 0 || b != 0 {
                    return false
                }
            }
        }
    }

    return true
}
满天都是小星星 2024-10-20 03:22:19

我不在我的 Mac 上,所以我无法测试这个(并且可能存在编译错误)。但一种方法可能是:

//The pixel format depends on what sort of image you're expecting. If it's RGBA, this should work
typedef struct
{
   uint8_t red;
   uint8_t green;
   uint8_t blue;
   uint8_t alpha;
} MyPixel_T;

UIImage *myImage = [self doTheThingToGetTheImage];
CGImageRef myCGImage = [myImage CGImage];

//Get a bitmap context for the image
CGBitmapContextRef *bitmapContext = 
CGBitmapContextFreate(NULL, CGImageGetWidth(myCGImage), CGImageGetHeight(myCGImage), 
                      CGImageGetBitsPerComponent(myCGImage), CGImageGetBytesPerRow(myCGImage),
                      CGImageGetColorSpace(myCGImage), CGImageGetBitmapInfo(myCGImage));

//Draw the image into the context
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGImageGetWidth(myCGImage), CGImageGetHeight(myCGImage)), myCGImage);

//Get pixel data for the image
MyPixel_T *pixels = CGBitmapContextGetData(bitmapContext);
size_t pixelCount = CGImageGetWidth(myCGImage) * CGImageGetHeight(myCGImage);

for(size_t i = 0; i < pixelCount; i++)
{
   MyPixel_T p = pixels[i];
   //Your definition of what's blank may differ from mine
   if(p.red > 0 && p.green > 0 && p.blue > 0 && p.alpha > 0)
      return NO;
}

return YES;

I'm not at my Mac, so I can't test this (and there are probably compile errors). But one method might be:

//The pixel format depends on what sort of image you're expecting. If it's RGBA, this should work
typedef struct
{
   uint8_t red;
   uint8_t green;
   uint8_t blue;
   uint8_t alpha;
} MyPixel_T;

UIImage *myImage = [self doTheThingToGetTheImage];
CGImageRef myCGImage = [myImage CGImage];

//Get a bitmap context for the image
CGBitmapContextRef *bitmapContext = 
CGBitmapContextFreate(NULL, CGImageGetWidth(myCGImage), CGImageGetHeight(myCGImage), 
                      CGImageGetBitsPerComponent(myCGImage), CGImageGetBytesPerRow(myCGImage),
                      CGImageGetColorSpace(myCGImage), CGImageGetBitmapInfo(myCGImage));

//Draw the image into the context
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGImageGetWidth(myCGImage), CGImageGetHeight(myCGImage)), myCGImage);

//Get pixel data for the image
MyPixel_T *pixels = CGBitmapContextGetData(bitmapContext);
size_t pixelCount = CGImageGetWidth(myCGImage) * CGImageGetHeight(myCGImage);

for(size_t i = 0; i < pixelCount; i++)
{
   MyPixel_T p = pixels[i];
   //Your definition of what's blank may differ from mine
   if(p.red > 0 && p.green > 0 && p.blue > 0 && p.alpha > 0)
      return NO;
}

return YES;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文