
- (int)getRGBAFromImage:(UIImage *)image atX:(int)xx andY:(int)yy { // [...] // What do I want to read about to help // me fill in this bit,here? // [...] int result = (red << 24) | (green << 16) | (blue << 8) | Alpha; return result;} 谢谢!
解决方法 FYI,我结合Keremk的答案和我的原始大纲,清理打印错误,一般化它返回一个颜色数组,并得到整个编译。这里是结果:+ (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)x andY:(int)y count:(int)count{ NSMutableArray *result = [NSMutableArray arrayWithCapacity:count]; // 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,height),imageRef); CGContextRelease(context); // Now your rawData contains the image data in the RGBA8888 pixel format. NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel; for (int i = 0 ; i < count ; ++i) { CGfloat Alpha = ((CGfloat) rawData[byteIndex + 3] ) / 255.0f; CGfloat red = ((CGfloat) rawData[byteIndex] ) / Alpha; CGfloat green = ((CGfloat) rawData[byteIndex + 1] ) / Alpha; CGfloat blue = ((CGfloat) rawData[byteIndex + 2] ) / Alpha; byteIndex += bytesPerPixel; UIcolor *acolor = [UIcolor colorWithRed:red green:green blue:blue Alpha:Alpha]; [result addobject:acolor]; } free(rawData); return result;} 总结 以上是内存溢出为你收集整理的如何从UIImage(Cocoa Touch)或CGImage(Core Graphics)获取像素数据?全部内容,希望文章能够帮你解决如何从UIImage(Cocoa Touch)或CGImage(Core Graphics)获取像素数据?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)