iphone – 从RGB数据创建图像?
发布时间:2020-12-15 01:50:19 所属栏目:百科 来源:网络整理
导读:我真的遇到了麻烦.我有一些原始的rgb数据,值从0到255,并希望在iphone上显示为图像,但无法找到如何做到这一点.有人可以帮忙吗?我想我可能需要使用CGImageCreate,但只是不明白.试着看着班级参考,感觉很困惑. 我想要的只是从一些计算生成的10×10灰度图像,如果
我真的遇到了麻烦.我有一些原始的rgb数据,值从0到255,并希望在iphone上显示为图像,但无法找到如何做到这一点.有人可以帮忙吗?我想我可能需要使用CGImageCreate,但只是不明白.试着看着班级参考,感觉很困惑.
我想要的只是从一些计算生成的10×10灰度图像,如果有一种简单的方法来创建一个png或者很棒的东西. 解决方法
一个非常原始的例子,类似于Mats的建议,但这个版本使用外部像素缓冲区(pixelData):
const size_t Width = 10; const size_t Height = 10; const size_t Area = Width * Height; const size_t ComponentsPerPixel = 4; // rgba uint8_t pixelData[Area * ComponentsPerPixel]; // fill the pixels with a lovely opaque blue gradient: for (size_t i=0; i < Area; ++i) { const size_t offset = i * ComponentsPerPixel; pixelData[offset] = i; pixelData[offset+1] = i; pixelData[offset+2] = i + i; // enhance blue pixelData[offset+3] = UINT8_MAX; // opaque } // create the bitmap context: const size_t BitsPerComponent = 8; const size_t BytesPerRow=((BitsPerComponent * Width) / 8) * ComponentsPerPixel; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef gtx = CGBitmapContextCreate(&pixelData[0],Width,Height,BitsPerComponent,BytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast); // create the image: CGImageRef toCGImage = CGBitmapContextCreateImage(gtx); UIImage * uiimage = [[UIImage alloc] initWithCGImage:toCGImage]; NSData * png = UIImagePNGRepresentation(uiimage); // remember to cleanup your resources! :) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |