美文网首页OpenGL
iOS遍历像素

iOS遍历像素

作者: funnythansl | 来源:发表于2017-05-22 19:02 被阅读97次

我们的需求是获取图片的透明区域

<p><code>- (UIImage *)crateImage:(UIImage *)image{
CGImageRef cgimage = [image CGImage];
size_t width = CGImageGetWidth(cgimage); // 图片宽度
size_t height = CGImageGetHeight(cgimage); // 图片高度
unsigned char *data = calloc(width * height * 4, sizeof(unsigned char)); // 取图片首地址
size_t bitsPerComponent = 8; // r g b a 每个component bits数目
size_t bytesPerRow = width * 4; // 一张图片每行字节数目 (每个像素点包含r g b a 四个字节)
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); // 创建rgb颜色空间

CGContextRef context =
CGBitmapContextCreate(data,
                      width,
                      height,
                      bitsPerComponent,
                      bytesPerRow,
                      space,
                      kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgimage);
NSMutableArray *xArr = [[NSMutableArray alloc]init];
NSMutableArray *yArr = [[NSMutableArray alloc]init];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
   
for (size_t i = 0; i < height; i++)
{
    for (size_t j = 0; j < width; j++)
    {
        size_t pixelIndex = i * width * 4 + j * 4;
        
        int alpha = data[pixelIndex];
        int red = data[pixelIndex+1];
        int green = data[pixelIndex + 2];
        int blue = data[pixelIndex + 3];
        UIColor *color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:
                         (blue/255.0f) alpha:(alpha/255.0f)];
        UIColor *color1 = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.00];
        if ([self isTheSameColor2:color anotherColor:color1]) {

            int x = (int) j;
            int y = (int) i;
            [xArr addObject:[NSNumber numberWithInt:x]];
            [yArr addObject:[NSNumber numberWithInt:y]];
        }

    }
}
    int xBig = [[xArr valueForKeyPath:@"@max.intValue"] intValue];
    int xMin = [[xArr valueForKeyPath:@"@min.intValue"] intValue];;
    NSLog(@"Xmax = %d   Xmin = %d",xBig,xMin);
    self->maxX = xBig;
    self->minX = xMin;
    
    int yBig = [[yArr valueForKeyPath:@"@max.intValue"] intValue];
    int yMin = [[yArr valueForKeyPath:@"@min.intValue"] intValue];
    self->maxY = yBig;
    self->minY = yMin;
    NSLog(@"Ymax = %d   Ymin = %d",yBig,yMin);
    dispatch_async(dispatch_get_main_queue(), ^{
        [self createPic];
    });
   });
    cgimage = CGBitmapContextCreateImage(context);
image = [UIImage imageWithCGImage:cgimage];
return image;

}

  • (BOOL) isTheSameColor2:(UIColor)color1 anotherColor:(UIColor)color2
    {
    if (CGColorEqualToColor(color1.CGColor, color2.CGColor))
    {
    return YES;
    }
    else
    {
    return NO;
    }
    }</code></p>

相关文章

  • iOS遍历像素

    我们的需求是获取图片的透明区域 - (UIImage *)crateImage:(UIImage *)image{...

  • iOS 遍历图片像素

    像素是位图的基本组成单位,是图片上的一个个小方块,这些小方块都有一个明确的位置和被分配的色彩数值,小方格颜色和位置...

  • 像素遍历

    当处理图片的像素值时,如变化i=(i/10)*10。没有必要对每一个像素值进行这个操作。可以创建一个表,如果是灰度...

  • 高效遍历像素

    高效遍历像素注意事项:1、at方法在需要随机访问像素的时候使用,绝不要在扫描图像时使用;2、使用较短的循环和多条语...

  • 使用 CIFilter 实现 UIView 镂空效果

    在 iOS 中,并没有原生支持 UIView 镂空效果的接口。 目前网上能搜到的比较主流的一个方案是遍历每一个像素...

  • Android和IOS使用的尺寸单位

    IOS px:像素 ppi:pixels per inch,每英寸像素数 pt:point,ios开发使用的单位 ...

  • iOS开发遍历集合(NSArray,NSDictionary、N

    iOS开发遍历集合(NSArray,NSDictionary、NSSet)方法总结 iOS开发遍历集合(NSArr...

  • 004 图像像素的读写操作

    本节内容:图像像素操作:遍历与访问 C++ 在C++中访问OpenCV Mat对象每个像素点的像素值有两种方式: ...

  • appicon

    型号iphone点pt尺寸(像素)尺寸(像素) iphone iPhone Notification iOS 7-...

  • media-像素与屏幕

    Ios尺寸表 1、 piexl 像素知识 逻辑像素与物理像素的关系 px逻辑像素:浏览器使用的抽象单位 dp,pt...

网友评论

    本文标题:iOS遍历像素

    本文链接:https://www.haomeiwen.com/subject/qkjixxtx.html