我正在尝试保存UIIMage,然后检索并显示它。通过在图像包中保存图像,从那里检索并显示图像,我获得了成功。我的问题来自于我尝试将图像保存到磁盘(将其转换为NSData),然后检索它。我将图像转换为NSData,就像这样......
NSData* topImageData = UIImageJPEGRepresentation(topImage, 1.0);
然后我就把它写到磁盘上......
[topImageData writeToFile:topPathToFile atomically:NO];
然后我试图像这样检索它......
topSubView.image = [[UIImage alloc] initWithContentsOfFile:topPathToFile];
不返回任何图像(大小为零)。所以我试过......
NSData *data = [[NSData alloc] initWithContentsOfFile:topPathToFile];
topSubView.image = [[UIImage alloc] initWithData:data];
无济于事。当我单步执行调试器时,我确实看到数据包含正确的字节数,但我很困惑为什么我的图像没有被创建。我错过了一步吗?在转换为图像之前,我是否需要对NSData执行某些操作?
答案 0 :(得分:33)
试试这段代码。这对我有用。
保存数据。
创建保存图像的路径。
let libraryDirectory = NSSearchPathForDirectoriesInDomains(.libraryDirectory,
.userDomainMask,
true)[0]
let libraryURL = URL(fileURLWithPath: libraryDirectory, isDirectory: true)
let fileURL = libraryURL.appendingPathComponent("image.data")
将图像转换为Data对象并将其保存到文件中。
let data = UIImageJPEGRepresentation(myImage, 1.0)
try? data?.write(to: fileURL)
检索已保存的图片
let newImage = UIImage(contentsOfFile: fileURL.relativePath)
创建一个imageview并使用检索到的图像加载它。
let imageView = UIImageView(image: newImage)
self.view.addSubview(imageView)
答案 1 :(得分:17)
您应该能够使用UIImage的类方法
[UIImage imageWithContentsOfFile:topPathToFile];
OR
[UIImage imageWithData:data];
这不起作用吗?
希望这有帮助!
答案 2 :(得分:6)
以防这有助于某人,从iOS6我们有imageWithData:scale方法。要从NSData对象获取具有正确比例的UIImage,请使用该方法,声明用于存储原始图像的比例。例如:
CGFloat screenScale = [[UIScreen mainScreen] scale];
UIImage *image = [UIImage imageWithData:myimage scale:screenScale];
此处,myimage是存储原始图像的NSData对象。在示例中,我使用了屏幕的比例。如果您对原始图像使用其他比例,请改用该值。
答案 3 :(得分:0)
检查出来:http://www.nixwire.com/getting-uiimage-to-work-with-nscoding-encodewithcoder/。
我认为解决问题的方法正是如此。你不能只用NSData制作图像,即使这是常识所暗示的。你必须使用UIImagePNGRepresentation。如果这有帮助,请告诉我。
答案 4 :(得分:0)
使用if-let阻止数据以防止应用崩溃&安全执行代码,因为函数UIImagePNGRepresentation返回一个可选值。
if let img = UIImage(named: "TestImage.png") {
if let data:Data = UIImagePNGRepresentation(img) {
// Handle operations with data here...
}
}
注意:数据是Swift 3+类。使用数据而不是NSData Swift 3 +
通用图像操作(如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...
}
}
使用扩展名:
extension UIImage {
var pngRepresentationData: Data? {
return UIImagePNGRepresentation(img)
}
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...
}
}