You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin中UIImage转Bitmap实现咨询:iOS端无对应转换方法

iOS 实现 UIImage 到 Bitmap 等效转换的解决方案

嘿,我刚好之前也踩过这个坑!iOS里没有和安卓Bitmap完全对应的类,不过我们可以用CGImage(Core Graphics框架里的图像对象)或者直接通过UIImage的相关API来实现和安卓BitmapFactory.DecodeByteArray()等效的功能,分两种常见场景给你拆解:

场景1:从原始像素字节数组转换(对应安卓处理Raw像素数据)

如果你的字节数组是未经压缩的原始像素数据(比如RGBA格式的像素字节流),需要手动创建位图上下文来生成图像:

Swift 代码示例

func image(fromRawPixelBytes bytes: [UInt8], width: Int, height: Int) -> UIImage? {
    let bitsPerComponent = 8
    let bytesPerPixel = 4 // RGBA格式每个像素占4字节
    let bytesPerRow = bytesPerPixel * width
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    
    guard let context = CGContext(data: UnsafeMutableRawPointer(mutating: bytes),
                                  width: width,
                                  height: height,
                                  bitsPerComponent: bitsPerComponent,
                                  bytesPerRow: bytesPerRow,
                                  space: colorSpace,
                                  bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
        return nil
    }
    
    guard let cgImage = context.makeImage() else {
        return nil
    }
    
    return UIImage(cgImage: cgImage)
}

Objective-C 代码示例

- (UIImage *)imageFromRawPixelBytes:(NSData *)pixelData width:(NSInteger)width height:(NSInteger)height {
    NSInteger bitsPerComponent = 8;
    NSInteger bytesPerPixel = 4;
    NSInteger bytesPerRow = bytesPerPixel * width;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    CGContextRef context = CGBitmapContextCreate((void *)pixelData.bytes,
                                                 width,
                                                 height,
                                                 bitsPerComponent,
                                                 bytesPerRow,
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast);
    if (!context) {
        CGColorSpaceRelease(colorSpace);
        return nil;
    }
    
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    
    CGImageRelease(cgImage);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    
    return image;
}

场景2:从压缩图片字节数组转换(对应安卓处理JPEG/PNG数据流)

如果你的字节数组是JPEG、PNG等压缩后的图片数据,那就简单多了,直接用UIImage的初始化方法就能搞定,和安卓BitmapFactory.DecodeByteArray()处理压缩数据的逻辑完全一致:

Swift 代码示例

func image(fromCompressedBytes bytes: [UInt8]) -> UIImage? {
    let data = Data(bytes: bytes)
    return UIImage(data: data)
}

Objective-C 代码示例

- (UIImage *)imageFromCompressedBytes:(NSData *)compressedData {
    return [UIImage imageWithData:compressedData];
}

补充说明

  • 如果你需要对生成的图像做进一步的像素级操作,CGImage就是iOS里和安卓Bitmap功能最接近的对象,可以通过它来获取像素数据、修改像素等。
  • 注意内存管理:Core Graphics的对象(比如CGContext、CGImage)在Objective-C里需要手动调用Release,Swift里则由ARC自动管理,但如果涉及到Unsafe指针操作,还是要留意内存安全。

内容的提问来源于stack exchange,提问作者Mannu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:09:40