iOS遍歷像素

我們的需求是獲取圖片的透明區(qū)域

<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 每個(gè)component bits數(shù)目
size_t bytesPerRow = width * 4; // 一張圖片每行字節(jié)數(shù)目 (每個(gè)像素點(diǎn)包含r g b a 四個(gè)字節(jié))
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); // 創(chuàng)建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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容