NSData 到 UIImage

发布于 2024-08-20 23:47:00 字数 722 浏览 9 评论 0原文

我正在尝试保存 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 执行某些操作吗?

I'm trying to save a UIIMage and then retrieve it and display it. I've been successful by saving an image in the bundle, retrieving it from there and displaying. My problem comes from when I try to save an image to disk (converting it to NSData), then retrieving it. I convert the image to NSData like so...

NSData* topImageData = UIImageJPEGRepresentation(topImage, 1.0);

then I write it to disk like so...

[topImageData writeToFile:topPathToFile atomically:NO];

then I tried to retrieve it like so...

topSubView.image = [[UIImage alloc] initWithContentsOfFile:topPathToFile];

which returns no image (the size is zero). so then I tried...

NSData *data = [[NSData alloc] initWithContentsOfFile:topPathToFile];
topSubView.image = [[UIImage alloc] initWithData:data];

to no avail. When I step through the debugger I do see that data contains the correct number of bytes but I'm confused as to why my image is not being created. Am I missing a step? Do I need to do something with NSData before converting to an image?

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

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

发布评论

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

评论(5

驱逐舰岛风号 2024-08-27 23:47:00

试试这个代码。这对我有用。

保存数据。

创建路径来保存图像。

let libraryDirectory = NSSearchPathForDirectoriesInDomains(.libraryDirectory,
                                                               .userDomainMask,
                                                               true)[0]
let libraryURL = URL(fileURLWithPath: libraryDirectory, isDirectory: true)
let fileURL = libraryURL.appendingPathComponent("image.data")

将图像转换为数据对象并将其保存到文件中。

let data = UIImageJPEGRepresentation(myImage, 1.0)
try? data?.write(to: fileURL)

检索保存的图像

let newImage = UIImage(contentsOfFile: fileURL.relativePath)

创建一个图像视图并使用检索到的图像加载它。

let imageView = UIImageView(image: newImage)
self.view.addSubview(imageView)

Try this code. This worked for me.

Saving the data.

create path to save the image.

let libraryDirectory = NSSearchPathForDirectoriesInDomains(.libraryDirectory,
                                                               .userDomainMask,
                                                               true)[0]
let libraryURL = URL(fileURLWithPath: libraryDirectory, isDirectory: true)
let fileURL = libraryURL.appendingPathComponent("image.data")

convert the image to a Data object and save it to the file.

let data = UIImageJPEGRepresentation(myImage, 1.0)
try? data?.write(to: fileURL)

retrieving the saved image

let newImage = UIImage(contentsOfFile: fileURL.relativePath)

Create an imageview and load it with the retrieved image.

let imageView = UIImageView(image: newImage)
self.view.addSubview(imageView)
め七分饶幸 2024-08-27 23:47:00

您应该能够使用 UIImage 的类方法

[UIImage imageWithContentsOfFile:topPathToFile]; 

或者

[UIImage imageWithData:data];

这不起作用吗?

希望这有帮助!

You should be able to use class methods of UIImage

[UIImage imageWithContentsOfFile:topPathToFile]; 

OR

[UIImage imageWithData:data];

Did that not work?

Hope this helps!

你又不是我 2024-08-27 23:47:00

以防万一这对某人有帮助,从 iOS6 开始,我们有 imageWithData:scale 方法。要从 NSData 对象获取具有正确比例的 UIImage,请使用该方法,声明用于存储原始图像的比例。例如:

CGFloat screenScale = [[UIScreen mainScreen] scale];
UIImage *image = [UIImage imageWithData:myimage scale:screenScale];

这里,myimage是存储原始图像的NSData对象。在示例中,我使用了屏幕的比例。如果您对原始图像使用其他比例,请改用该值。

Just in case this helps someone, from iOS6 we have the imageWithData:scale method. To get an UIImage with the right scale from an NSData object, use that method, declaring the scale you use to store the original image. For example:

CGFloat screenScale = [[UIScreen mainScreen] scale];
UIImage *image = [UIImage imageWithData:myimage scale:screenScale];

Here, myimage is the NSData object where you stored the original image. In the example, I used the scale of the screen. If you use another scale for the original image, use that value instead.

霊感 2024-08-27 23:47:00

看看这个: http://www.nixwire.com/获取 uiimage-to-work-with-nscoding-encodewithcoder/

它正是我认为解决您问题的方法。你不能仅仅用 NSData 制作图像,即使这是常识所暗示的。您必须使用 UIImagePNGRepresentation。让我知道这是否有帮助。

Check this out: http://www.nixwire.com/getting-uiimage-to-work-with-nscoding-encodewithcoder/.

It has exactly what I think the solution to your problem is. You can't just make an image out of NSData, even though that's what common sense suggests. You have to use UIImagePNGRepresentation. Let me know if this helps.

柏林苍穹下 2024-08-27 23:47:00

将 if-let 块与数据一起使用以防止应用程序崩溃安全执行代码,因为函数 UIImagePNGRepresentation 返回一个可选值。

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

注意:Data 是 Swift 3+ 类。使用 Data 而不是 NSData
斯威夫特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...
     }
}

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

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+

Generic image operations (like png & jpg both):

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