从 UIImage 获取底层 NSData

发布于 2024-10-11 03:52:18 字数 223 浏览 2 评论 0原文

我可以使用 [UIImage imageWithData:][UIImage initWithData:] 方法从 NSData 创建 UIImage

我想知道是否可以从现有的 UIImage 中获取 NSData ? NSData *myData = [myImage getData]; 行上的内容

I can create UIImage from NSData using [UIImage imageWithData:] or [UIImage initWithData:] methods.

I wonder if I can get the NSData back from an existing UIImage?
Something on the line of NSData *myData = [myImage getData];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

长途伴 2024-10-18 03:52:18

Objective-C

NSData *imageData = UIImageJPEGRepresentation(image, 0.7); // 0.7 is JPG quality

NSData *imageData = UIImagePNGRepresentation(image);

取决于您是否想要 PNG 格式或 JPG 格式的数据。

Swift

在 Swift 的现代版本中,上述方法已分别替换为

let data = image.jpegData(compressionQuality: 0.7)

let data = image.pngData()

请注意,两者都返回一个可选值(Data?)。

Objective-C

NSData *imageData = UIImageJPEGRepresentation(image, 0.7); // 0.7 is JPG quality

or

NSData *imageData = UIImagePNGRepresentation(image);

Depending if you want your data in PNG format or JPG format.

Swift

In modern versions of Swift, the methods above have been replaced with

let data = image.jpegData(compressionQuality: 0.7)

and

let data = image.pngData()

respectively.

Note that both return an optional value (Data?).

温柔一刀 2024-10-18 03:52:18

当使用 init(data: originalData) 初始化 UIImage 对象时,originalData 将被转换为某种内部格式的原始数据。稍后可以使用 rawData 检索这些数据,

let rawData = myImage.cgImage?.dataProvider?.data as Data?

但是由于 rawData 是原始数据,因此它会比使用 UIImagePNGRepresentation 时更大。

When initialising a UIImage object with init(data: originalData), that originalData will be converted into raw data in some kind of internal format. These data can be retrieved later with

let rawData = myImage.cgImage?.dataProvider?.data as Data?

However because the rawData is raw, it is going to be even larger than when using UIImagePNGRepresentation.

夏天碎花小短裙 2024-10-18 03:52:18

斯威夫特4.2

let dataPng = image.pngData() // return image as PNG. May return nil if image has no CGImageRef or invalid bitmap format

let dataJpg = image.jpegData(compressionQuality: 1) // return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)

Swift 4.2

let dataPng = image.pngData() // return image as PNG. May return nil if image has no CGImageRef or invalid bitmap format

let dataJpg = image.jpegData(compressionQuality: 1) // return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)
忆依然 2024-10-18 03:52:18

只是因为我偶然发现了这一点,而且我喜欢 swift :)

这是 Caroline 帖子的快速翻译。

var imageData = UIImagePNGRepresentation(image)

或者

var imageData = UIImageJPEGRepresentation(image, 0.7) 

Just because I stumbled upon this and i like swift :)

Here is the swift translation of Caroiline's post.

var imageData = UIImagePNGRepresentation(image)

Or

var imageData = UIImageJPEGRepresentation(image, 0.7) 
秋千易 2024-10-18 03:52:18

您可以预期 UIImage 是一个为显示而格式化的对象,因此不会使用原始数据(可能是 PNG 或 JPEG 格式),而更可能是像素数组或其他内部格式。换句话说,UIImage(data: foo) 不会保留 foo

  1. 如果你只是想在程序的其他地方使用它,原始的 UIImage 就可以了(我想这里实际上并非如此)

  2. 如果你想序列化,UIImagePNGRepresentation(...) 可以工作,但如果原始图像是 JPEG,则尺寸会过大; UIImageJPEGRepresentation(...) 通常会导致数据稍微过大,并且如果您的原始数据是 PNG,则数据会略有丢失。应该可以根据图像的显示方式和您期望提供的格式来选择一种。如果您碰巧使用 PNG 输入并希望输出 PNG,您应该获得良好的文件大小和几乎相同的数据,除了特殊的 PNG 块。

  3. 如果您想获得原始数据的精确副本(也许在缩略图后保存文件,或对其进行 SHA1 处理),则需要单独保留它。你可能会这样做:

    var image:UIImage
    var imageData:NSData {
        没有设置{
            图像 = UIImage(数据: imageData)
        }
    }
    

You can expect that a UIImage is an object formatted for display and so won't be using the original data (which is probably in PNG or JPEG format) but more likely a pixel array or some other internal format. In other words, UIImage(data: foo) will not retain foo.

  1. If you just want to use it elsewhere in your program, the original UIImage will do fine (I presume that's not actually the case here)

  2. If you want to serialise, UIImagePNGRepresentation(...) will work but will be oversized if the original was a JPEG; UIImageJPEGRepresentation(...) will often result in slightly oversize data and is slightly lossy if your original was PNG. It should be okay to pick one based on the way the image will be displayed and the format you expect to be provided. If you happen to be using PNG in and want PNG out, you should get a good file size and almost identical data, special PNG chunks aside.

  3. If you want to get an exact copy of the original data (perhaps to save a file after thumbnailing, or to SHA1 it), then you need to retain it separately. You might do something like:

    var image:UIImage
    var imageData:NSData {
        didSet {
            image = UIImage(data: imageData)
        }
    }
    
薄凉少年不暖心 2024-10-18 03:52:18

我发现序列化/反序列化 UIImage(通过数据)的唯一解决方案是使用此解决方案

然后,无论 UIImage 是如何创建的,您都可以通过使用 UIImage 上的扩展方法来序列化/反序列化:

let originalImage: UIImage = ...
let cgData = image.cgImage!.png!
let image = UIImage(data: cgData)!

The only solution I found to serialize/unserialize a UIImage (via Data) is by using this solution.

You can then serialize/unserialize regardless of how the UIImage was created by using the extension method on UIImage:

let originalImage: UIImage = ...
let cgData = image.cgImage!.png!
let image = UIImage(data: cgData)!
相思碎 2024-10-18 03:52:18

自从给出上述答案以来,事情已经发生了变化,对于那些仍在寻找的人来说,因为他们分享 CipherCom 的担忧:iOS 5 添加了 CIImage 属性

Things have changed since the above answer was given, for those still looking because they share CipherCom's concern: iOS 5 has added the CIImage property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文