如何使用Swift保存远程图像?

编程入门 行业动态 更新时间:2024-10-09 10:26:41
本文介绍了如何使用Swift保存远程图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Swift显示和保存图像。在第一次点击时,它会在imageview上显示远程图像,在第二次点击时显示空白图像视图而不是它应该是在首次点击时保存的本地图像。

I'm trying to display and save images with Swift. On first hit, it shows the remote image on imageview, on second hit it shows blank imageview instead of it should be local image which saved on first hit.

var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String var imagePath = paths.stringByAppendingPathComponent("images/\(id)/logo.jpg" ) var checkImage = NSFileManager.defaultManager() if (checkImage.fileExistsAtPath(imagePath)) { let getImage = UIImage(contentsOfFile: imagePath) self.image?.image = getImage } else { dispatch_async(dispatch_get_main_queue()) { let getImage = UIImage(data: NSData(contentsOfURL: NSURL(string: remoteImage))) UIImageJPEGRepresentation(getImage, 100).writeToFile(imagePath, atomically: true) self.image?.image = getImage } }

编辑:这一个w orked for me。

This one worked for me.

var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String var dirPath = paths.stringByAppendingPathComponent("images/\(id)" ) var imagePath = paths.stringByAppendingPathComponent("images/\(id)/logo.jpg" ) var checkImage = NSFileManager.defaultManager() if (checkImage.fileExistsAtPath(imagePath)) { let getImage = UIImage(contentsOfFile: imagePath) self.image?.image = getImage } else { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { checkImage.createDirectoryAtPath(dirPath, withIntermediateDirectories: true, attributes: nil, error: nil) let getImage = UIImage(data: NSData(contentsOfURL: NSURL(string: remoteImage))) UIImageJPEGRepresentation(getImage, 100).writeToFile(imagePath, atomically: true) dispatch_async(dispatch_get_main_queue()) { self.image?.image = getImage return } } }

推荐答案

要回答您的主要问题,您将调用错误的 UIImage 初始化程序。你应该在swift 2中调用 UIImage(contentsOfFile:imagePath),在swift 3中调用 UIImage(contentsOf:imagePath)。

To answer your main question, you're calling the wrong UIImage initializer. You should be calling UIImage(contentsOfFile: imagePath) in swift 2 and UIImage(contentsOf: imagePath) in swift 3.

此外,您似乎正在尝试使用 dispatch_async (或 DispatchQueue ,但是你将它传递给主队列,所以你实际上是用它来阻止主/ UI线程。您应该将其分配给其中一个后台队列,然后在UI中实际设置图像时调度回主队列:

Additionally, it looks like you're trying to do your remote fetch in the background with dispatch_async (or DispatchQueue in swift 3), but you're passing it the main queue, so you're actually blocking the main/UI thread with that. You should dispatch it to one of the background queues instead and then dispatch back to the main queue when you actually set the image in your UI:

Swift 3:

DispatchQueue.global(qos: DispatchQoS.background.qosClass).async { do { let data = try Data(contentsOf: URL(string: self.remoteImage)!) let getImage = UIImage(data: data) try UIImageJPEGRepresentation(getImage!, 100)?.write(to: imagePath) DispatchQueue.main.async { self.image?.image = getImage return } } catch { return } }

Swift 2:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { let getImage = UIImage(data: NSData(contentsOfURL: NSURL(string: self.remoteImage))) UIImageJPEGRepresentation(getImage, 100).writeToFile(imagePath, atomically: true) dispatch_async(dispatch_get_main_queue()) { self.image?.image = getImage return } }

@ Rob的回答 re:获取远程图像并保存它是最好的方法。

@Rob's answer re: fetching your remote image and saving it is really the best way to do this.

更多推荐

如何使用Swift保存远程图像?

本文发布于:2023-11-13 03:09:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   图像   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!