V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
CSGO
V2EX  ›  iDev

iOS 上有什么办法图片转数据?

  •  
  •   CSGO ·
    wyfang · 107 天前 · 1123 次点击
    这是一个创建于 107 天前的主题,其中的信息可能已经有所发展或是发生改变。
    帮 iOS 开发问的。
    我之前用 AI 按照自己要求,写了一个 html 页面,可以实现我任意上传一个图片,比如 2 * 2 像素的 png 图片,一共四个像素,然后转换成类似 0x00 这样的数据组,是个 16 阶颜色的数据: https://www.wangyifang.com/web/img2hex/index.html

    iOS 这边问了 AI 用到类似这样的:
    -------
    在 iOS 中,你可以使用 UIImage 来获取图片的数据。这里是一种常用的方法:
    首先,你需要使用 UIImage 来获取你的图片,然后使用 UIImageJPEGRepresentation 或 UIImagePNGRepresentation 来将图片转化为 NSData 。
    例如:

    swift
    if let image = UIImage(named: "your_image") {
    let imageData = UIImagePNGRepresentation(image)
    }

    UIImageJPEGRepresentation 和 UIImagePNGRepresentation 的区别主要在于它们压缩图像数据的方式不同。JPEG 通常用于照片,而 PNG 通常用于带有透明度的图像。
    如果你需要进一步处理图像数据,例如获取照片的具体像素信息,你可以将 NSData 转换为 CGImage ,然后通过 CGImage 来分析图像的每一个像素。
    需要注意的是,所有的这些操作都能消耗大量的内存和 CPU ,如果你处理的图像非常大,那么可能需要考虑使用更加高级的图像处理库,例如 Core Image 或者 GPUImage 。
    希望以上信息能帮助到你!
    -------

    但问题出现在这个东西 iOS 说用的是系统的,这导致现在比如在 iOS17.2.1 上会出现获取到图片数据是错误的。表现在如果有个 50*50 像素的带透明的 png ,中间有个正方形,那么获取到数据理论应该前后都有大量 0000 ,因为 RGBA 都是 0 ,结果获取到的数据前面会有其他数值,这导致再输出图像前面或者说说大量原本完全透明的地方出现大量比较随机的颜色。

    现在想问的是,有没办法自己用什么办法能获取图片的像素信息?或者说 iOS 有没办法类似 html 那样能直接把图片转成 0x00 的?
    rshinich
        1
    rshinich  
       107 天前
    试试直接用 CGImage 呢?然后用这个: https://developer.apple.com/documentation/coregraphics/cgdataprovider
    653513754
        2
    653513754  
       107 天前
    ```
    UIImage *origImg = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%s",imgPath]]; //加载图片

    CGSize size = [origImg size];

    int width = size.width;
    int height = size.height;

    uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

    memset(pixels, 0, width * height * sizeof(uint32_t));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
    kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), [origImg CGImage]);

    int tt = 1;
    CGFloat intensity;
    int bw;


    for (int y = 0; y < height; y++)
    {
    for (int x = 0; x < width; x++)
    {
    uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x]; // rgbaPixel[tt] + rgbaPixel[tt + 1] + rgbaPixel[tt + 2] 像素点

    }
    }


    CGImageRef image = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    free(pixels);
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2798 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:17 · PVG 23:17 · LAX 08:17 · JFK 11:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.