iOS:循环显示图像的像素 [英] iOS : Looping over the pixels of an Image

查看:99
本文介绍了iOS:循环显示图像的像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张相同格式和大小的图片。

I've two images of the same format and size.

我想循环显示每张图片的像素,找到最左上角和右上角差异坐标。

I would like to loop over the pixels of each image and find the most top left and buttom right difference coordinates.

差异被认为是具有相同坐标的两个像素的像素颜色。

Difference is considered the pixel color of the two pixel with the same coordinates.

你能不能请给我一个示例代码循环通过图像像素并获取其颜色值?

Can you please provide me a sample code looping through Image pixels and getting its color value ?

推荐答案

这对我有用(这是使用ARC):

This works for me (this is using ARC):

- (BOOL) pointInside: (CGPoint) point withEvent: (UIEvent *) event {
    CGRect r = CGRectZero ;

    r.size =[self size] ;

    CGFloat red ;
    CGFloat green ;
    CGFloat blue ;
    CGFloat alpha ;

    if (point.x < 0 || point.x > r.size.width || point.y < 0 || point.y > r.size.height) {
        return NO ;
    }

    [inspector colorAt:point red:&red green:&green blue:&blue alpha:&alpha] ;

    return alpha > 0.01f ;
}

这就是魔术:)

// CGImageInspection.h
#import <Foundation/Foundation.h>

@interface CGImageInspection : NSObject

+ (CGImageInspection *) imageInspectionWithCGImage: (CGImageRef) imageRef ;

- (void) colorAt: (CGPoint) location
             red: (CGFloat *) red 
           green: (CGFloat *) green
            blue: (CGFloat *) blue
           alpha: (CGFloat *) alpha ;

@end

以及相应的实施:

// CGImageInspection.mm
@interface CGImageInspection ()

- (id) initWithCGImage: (CGImageRef) imageRef ;

@property (assign, nonatomic) CGContextRef  context ;
@property (assign, nonatomic) void *        pixels ;
@property (assign, nonatomic) NSUInteger    bytesPerRow ;
@property (assign, nonatomic) NSUInteger    bytesPerPixel ;

@end

@implementation CGImageInspection

@synthesize context ;
@synthesize pixels ;
@synthesize bytesPerRow ;
@synthesize bytesPerPixel ;

- (void) dealloc {
    if (self.context) {
        ::CGContextRelease(self.context) ;
        self.context = 0 ;
    }

    if (self.pixels) {
        ::free(self.pixels) ;
        self.pixels = 0 ;
    }

    ::NSLog(@"CGImageInspection going away") ;
}

+ (CGImageInspection *) imageInspectionWithCGImage: (CGImageRef) imageRef {
    return [[CGImageInspection alloc] initWithCGImage:imageRef] ;
}

- (id) initWithCGImage: (CGImageRef) imageRef {

    if (self = [super init]) {
        size_t  pixelsWide = ::CGImageGetWidth(imageRef) ;
        size_t  pixelsHigh = ::CGImageGetHeight(imageRef) ;

        self.bytesPerPixel = 4 ;
        self.bytesPerRow = (pixelsWide * self.bytesPerPixel) ;
        int     bitmapByteCount   = (self.bytesPerRow * pixelsHigh) ;

        CGColorSpaceRef colorSpace= ::CGColorSpaceCreateDeviceRGB() ;

        if (colorSpace == 0) {
            return nil ;
        }

        self.pixels = ::calloc(bitmapByteCount, 1) ;
        if (self.pixels == 0) {
            ::CGColorSpaceRelease(colorSpace) ;
            return nil ;
        }

        self.context = ::CGBitmapContextCreate(
            self.pixels
        ,   pixelsWide
        ,   pixelsHigh
        ,   8      // bits per component
        ,   self.bytesPerRow
        ,   colorSpace
        ,   kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big) ;

        if (self.context == 0) {
            ::free(self.pixels) ;
            self.pixels = 0 ;
            ::CGColorSpaceRelease(colorSpace) ;
            return nil ;
        }
        ::CGContextDrawImage(context, (CGRect) {{0, 0}, {pixelsWide, pixelsHigh}}, imageRef) ;
        ::CGColorSpaceRelease(colorSpace) ;
    }

    return self ;

}

- (void) colorAt: (CGPoint) location
             red: (CGFloat *) red 
           green: (CGFloat *) green
            blue: (CGFloat *) blue
           alpha: (CGFloat *) alpha {

    int yy = (int) location.y ;
    int xx = (int) location.x ;

    int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel ;
    unsigned char * raw = (unsigned char *) self.pixels ;
    raw += byteIndex ;

    *red    = ((CGFloat) (*raw++)) / 255.0f ;
    *green  = ((CGFloat) (*raw++)) / 255.0f ;
    *blue   = ((CGFloat) (*raw++)) / 255.0f ;
    *alpha  = ((CGFloat) (*raw++)) / 255.0f ;
}

@end

希望这有帮助。

这篇关于iOS:循环显示图像的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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