iphone – 从RGB数据创建图像?

iphone – 从RGB数据创建图像?,第1张

概述我真的遇到了麻烦.我有一些原始的rgb数据,值从0到255,并希望在iphone上显示为图像,但无法找到如何做到这一点.有人可以帮忙吗?我想我可能需要使用CGImageCreate,但只是不明白.试着看着班级参考,感觉很困惑. 我想要的只是从一些计算生成的10×10灰度图像,如果有一种简单的方法来创建一个png或者很棒的东西. 一个非常原始的例子,类似于Mats的建议,但这个版本使用外部像素缓冲区 我真的遇到了麻烦.我有一些原始的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; // rgbauint8_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! :)
总结

以上是内存溢出为你收集整理的iphone – 从RGB数据创建图像?全部内容,希望文章能够帮你解决iphone – 从RGB数据创建图像?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1068673.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存