ios NSData 和 UIImage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/257400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSData and UIImage
提问by BlueDolphin
I am trying to load UIImage
object from NSData
, and the sample code was to NSImage
, I guess they should be the same. But just now loading the image, I am wondering what's the best to troubleshoot the UIImage
loading NSData
issue.
我正在尝试从 加载UIImage
对象NSData
,示例代码是到NSImage
,我想它们应该是相同的。但是刚刚加载图像,我想知道解决UIImage
加载NSData
问题的最佳方法是什么。
采纳答案by Noah Witherspoon
UIImage has an -initWithData:
method. From the docs: "The data in the data parameter must be formatted to match the file format of one of the system's supported image types."
UIImage 有一个 -initWithData:
方法。来自文档:“数据参数中的数据必须格式化以匹配系统支持的图像类型之一的文件格式。”
回答by b123400
I didn't try UIImageJPEGRepresentation()
before, but UIImagePNGRepresentation
works fine for me, and conversion between NSData
and UIImage
is dead simple:
我之前没有尝试UIImageJPEGRepresentation()
过,但UIImagePNGRepresentation
对我来说效果很好,并且在NSData
和之间的转换UIImage
非常简单:
NSData *imageData = UIImagePNGRepresentation(image);
UIImage *image=[UIImage imageWithData:imageData];
回答by Noah Witherspoon
Try this to convert an image to NSdata:
试试这个将图像转换为 NSdata:
UIImage *img = [UIImage imageNamed:@"image.png"];
NSData *data1 = UIImagePNGRepresentation(img);
回答by leonho
theData should be a NSData
object which already contains the data. You need to do the file loading/downloading to the NSData
object before it is used. You can inspect it by using NSLog
on theData and see if it contains the valid data.
theData 应该是一个NSData
已经包含数据的对象。NSData
在使用对象之前,您需要将文件加载/下载到对象。您可以通过NSLog
在 theData 上使用来检查它并查看它是否包含有效数据。
回答by Krunal
For safe execution of code, use if-let block with Data, as function UIImagePNGRepresentation returns, optional value.
为了代码的安全执行,使用带有数据的 if-let 块,作为函数 UIImagePNGRepresentation 返回,可选值。
if let img = UIImage(named: "Hello.png") {
if let data:Data = UIImagePNGRepresentation(img) {
// Handle operations with data here...
}
}
Note: Datais Swift 3 class. Use Data instead of NSData with Swift 3
注意:数据是 Swift 3 类。在 Swift 3 中使用 Data 而不是 NSData
Generic image operations (like png & jpg both):
通用图像操作(如 png 和 jpg 两者):
if let img = UIImage(named: "Hello.png") {
if let data:Data = UIImagePNGRepresentation(img) {
handleOperationWithData(data: data)
} else if let data:Data = UIImageJPEGRepresentation(img, 1.0) {
handleOperationWithData(data: data)
}
}
*******
func handleOperationWithData(data: Data) {
// Handle operations with data here...
if let image = UIImage(data: data) {
// Use image...
}
}
By using extension:
通过使用扩展:
extension UIImage {
var pngRepresentationData: Data? {
return UIImagePNGRepresentation(img)
}
var jpegRepresentationData: Data? {
return UIImageJPEGRepresentation(self, 1.0)
}
}
*******
if let img = UIImage(named: "Hello.png") {
if let data = img.pngRepresentationData {
handleOperationWithData(data: data)
} else if let data = jpegRepresentationData {
handleOperationWithData(data: data)
}
}
*******
func handleOperationWithData(data: Data) {
// Handle operations with data here...
if let image = UIImage(data: data) {
// Use image...
}
}