将UIImage转换成NSData,并在Swift中转换回UIImage ?

时间:2022-12-03 00:07:44

I'm trying to save a UIImage to NSData and then read the NSData back to a new UIImage in Swift. To convert the UIImage to NSData I'm using the following code:

我试图将一个UIImage保存到NSData,然后读取NSData到一个新的UIImage。要将UIImage转换成NSData,我使用以下代码:

let imageData: NSData = UIImagePNGRepresentation(myImage)

How do I convert imageData (i.e., NSData) back to a new UIImage?

如何转换imageData(例如。, NSData)返回到新的UIImage?

6 个解决方案

#1


85  

UIImage(data:imageData,scale:1.0) presuming the image's scale is 1.

UIImage(data:imageData,scale:1.0)假设图像的比例是1。

#2


40  

Thanks. Helped me a lot. Converted to Swift 3 and worked

谢谢。帮了我很多忙。转学为斯威夫特3并且工作

To save: let data = UIImagePNGRepresentation(image)

保存:让data = UIImagePNGRepresentation(image)

To load: let image = UIImage(data: data)

加载:让image = UIImage(数据:数据)

#3


27  

Use imageWithData: method, which gets translated to Swift as UIImage(data:)

使用imageWithData: method,它被翻译为UIImage(data:)

let image : UIImage = UIImage(data: imageData)

#4


4  

To save as data:

保存数据:

From StoryBoard, if you want to save "image" data on the imageView of MainStoryBoard, following codes will work.

如果您想在MainStoryBoard的imageView上保存“image”数据,可以使用以下代码。

let image = UIImagePNGRepresentation(imageView.image!) as NSData?

To load "image" to imageView: Look at exclamation point "!", "?" closely whether that is quite same as this one.

将“image”加载到imageView:看感叹号“!”, "?"这和这个是否完全一样。

imageView.image = UIImage(data: image as! Data)

"NSData" type is converted into "Data" type automatically during this process.

“NSData”类型在此过程中自动转换为“Data”类型。

#5


2  

For safe execution of code, use if-let block with Data to prevent app crash & , as function UIImagePNGRepresentation returns an optional value.

为了安全执行代码,使用带有数据的if-let块来防止应用程序崩溃&,因为函数UIImagePNGRepresentation返回一个可选值。

if let img = UIImage(named: "TestImage.png") {
    if let data:Data = UIImagePNGRepresentation(img) {
       // Handle operations with data here...         
    }
}

Note: Data is Swift 3+ class. Use Data instead of NSData with Swift 3+

注意:数据为Swift 3+类。使用Swift 3+使用数据代替NSData

Generic image operations (like png & jpg both):

通用图像操作(如png和jpg):

if let img = UIImage(named: "TestImage.png") {  //UIImage(named: "TestImage.jpg")
        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(self)
    }

    var jpegRepresentationData: Data? {
        return UIImageJPEGRepresentation(self, 1.0)
    }
}

*******
if let img = UIImage(named: "TestImage.png") {  //UIImage(named: "TestImage.jpg")
      if let data = img.pngRepresentationData {
              handleOperationWithData(data: data)     
      } else if let data = img.jpegRepresentationData {
              handleOperationWithData(data: data)     
     }
}

*******
func handleOperationWithData(data: Data) {
     // Handle operations with data here...
     if let image = UIImage(data: data) {
        // Use image...
     }
}

#6


0  

Now in Swift 4.2 you can use pngData() new instance method of UIImage to get the data from the image

在Swift 4.2中,可以使用UIImage的pngData()新实例方法从图像中获取数据

let profileImage = UIImage(named:"profile")!
let imageData = profileImage.pngData()

#1


85  

UIImage(data:imageData,scale:1.0) presuming the image's scale is 1.

UIImage(data:imageData,scale:1.0)假设图像的比例是1。

#2


40  

Thanks. Helped me a lot. Converted to Swift 3 and worked

谢谢。帮了我很多忙。转学为斯威夫特3并且工作

To save: let data = UIImagePNGRepresentation(image)

保存:让data = UIImagePNGRepresentation(image)

To load: let image = UIImage(data: data)

加载:让image = UIImage(数据:数据)

#3


27  

Use imageWithData: method, which gets translated to Swift as UIImage(data:)

使用imageWithData: method,它被翻译为UIImage(data:)

let image : UIImage = UIImage(data: imageData)

#4


4  

To save as data:

保存数据:

From StoryBoard, if you want to save "image" data on the imageView of MainStoryBoard, following codes will work.

如果您想在MainStoryBoard的imageView上保存“image”数据,可以使用以下代码。

let image = UIImagePNGRepresentation(imageView.image!) as NSData?

To load "image" to imageView: Look at exclamation point "!", "?" closely whether that is quite same as this one.

将“image”加载到imageView:看感叹号“!”, "?"这和这个是否完全一样。

imageView.image = UIImage(data: image as! Data)

"NSData" type is converted into "Data" type automatically during this process.

“NSData”类型在此过程中自动转换为“Data”类型。

#5


2  

For safe execution of code, use if-let block with Data to prevent app crash & , as function UIImagePNGRepresentation returns an optional value.

为了安全执行代码,使用带有数据的if-let块来防止应用程序崩溃&,因为函数UIImagePNGRepresentation返回一个可选值。

if let img = UIImage(named: "TestImage.png") {
    if let data:Data = UIImagePNGRepresentation(img) {
       // Handle operations with data here...         
    }
}

Note: Data is Swift 3+ class. Use Data instead of NSData with Swift 3+

注意:数据为Swift 3+类。使用Swift 3+使用数据代替NSData

Generic image operations (like png & jpg both):

通用图像操作(如png和jpg):

if let img = UIImage(named: "TestImage.png") {  //UIImage(named: "TestImage.jpg")
        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(self)
    }

    var jpegRepresentationData: Data? {
        return UIImageJPEGRepresentation(self, 1.0)
    }
}

*******
if let img = UIImage(named: "TestImage.png") {  //UIImage(named: "TestImage.jpg")
      if let data = img.pngRepresentationData {
              handleOperationWithData(data: data)     
      } else if let data = img.jpegRepresentationData {
              handleOperationWithData(data: data)     
     }
}

*******
func handleOperationWithData(data: Data) {
     // Handle operations with data here...
     if let image = UIImage(data: data) {
        // Use image...
     }
}

#6


0  

Now in Swift 4.2 you can use pngData() new instance method of UIImage to get the data from the image

在Swift 4.2中,可以使用UIImage的pngData()新实例方法从图像中获取数据

let profileImage = UIImage(named:"profile")!
let imageData = profileImage.pngData()