bhushan deshmukh
bhushan deshmukh

Reputation: 75

how to get pixel values(rgba) of image in objective c?

I am referring link below but getting error at

unsigned char* pixels = [image rgbaPixels];

saying

No visible @interface for 'UIImage' declares the selector 'rgbaPixels'

So, my question is how can I get pixel values(rgba) of image in objective c

Link reference

https://www.transpire.com/insights/blog/obtaining-luminosity-from-an-ios-camera

Upvotes: 0

Views: 812

Answers (1)

Quoc Nguyen
Quoc Nguyen

Reputation: 3007

You got the error

No visible @interface for 'UIImage' declares the selector 'rgbaPixels'

because rgbaPixels is the custom function, that the blogger written it himself.

You can create one yourself

First, create the Category of UIImage

.h file

@interface UIImage (ColorData)
- (unsigned char *)rgbaPixels;
@end

.m file

#import "UIImage+ColorData.h"

@implementation UIImage (ColorData)

- (unsigned char *)rgbaPixels {
    // First get the image into your data buffer
    CGImageRef imageRef = [self CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);
    return rawData;
}

@end

Upvotes: 3

Related Questions