我知道这是一个常见的问题,这个问题有很多答案.我已经使用了其中一些.虽然其中很多都是一样的.但令我伤心的是,他们都没有为我工作.以下我用过的代码.
-(void)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy
{
// First get the image into your data buffer
CGImageRef imageRef = [image 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);
// Now …Run Code Online (Sandbox Code Playgroud) 我收到'kCGImageAlphaPremultipliedLast'的未解决的标识符错误.斯威夫特找不到它.这可以在Swift中使用吗?
var gc = CGBitmapContextCreate(&pixelData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width*4, imageCS, bitmapInfo: kCGImageAlphaPremultipliedLast);
Run Code Online (Sandbox Code Playgroud)