Swift - 将图像从URL写入本地文件 [英] Swift - Write Image from URL to Local File
问题描述
我一直在学习快速,而我正在开发一个可以下载图片的OS X应用程序。
我已经能够解析JSON我正在寻找一个数组的URL如下:
func didReceiveAPIResults(results:NSArray){
println(results)
链接到结果中{
let stringLink = link as String
//检查以确保字符串实际上指向一个文件
如果stringLink.lowercaseString .rangeOfString(。jpg)!= nil {2
//将字符串转换为url
var imgURL:NSURL = NSURL(string:stringLink)!
//从URL下载图像的NSData表示
var request:NSURLRequest = NSURLRequest(URL:imgURL)
var urlConnection:NSURLConnection = NSURLConnection :request,delegate:self)!
//请求下载URL
NSURLConnection.sendAsynchronousRequest(request,queue:NSOperationQueue.mainQueue(),completionHandler:{(response:NSURLResponse !, data:NSData !, error:NSError!) - > ; void in
if!(错误?!= nil){
//将图像设置为请求的资源
var image = NSImage(data:data)
} else {
//如果请求失败...
println(error:\(error.localizedDescription))
}
})
}
}
}
所以现在我把我的图片定义为image ,但是我没有把握这里是如何保存这些文件到我的本地目录。
任何帮助这个问题将非常感激!
感谢,
tvick47
以下代码将在应用程序文档目录中的文件名filename.jpg下写入UIImage
var image = .... //但是你创建/获取一个UIImage
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0] as String
let destinationPath = documentsPath.stringByAppendingPathComponent( jpg)
UIImageJPEGRepresentation(image,1.0).writeToFile(destinationPath,atomically:true)
I've been learning swift rather quickly, and I'm trying to develop an OS X application that downloads images.
I've been able to parse the JSON I'm looking for into an array of URLs as follows:
func didReceiveAPIResults(results: NSArray) {
println(results)
for link in results {
let stringLink = link as String
//Check to make sure that the string is actually pointing to a file
if stringLink.lowercaseString.rangeOfString(".jpg") != nil {2
//Convert string to url
var imgURL: NSURL = NSURL(string: stringLink)!
//Download an NSData representation of the image from URL
var request: NSURLRequest = NSURLRequest(URL: imgURL)
var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)!
//Make request to download URL
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if !(error? != nil) {
//set image to requested resource
var image = NSImage(data: data)
} else {
//If request fails...
println("error: \(error.localizedDescription)")
}
})
}
}
}
So at this point I have my images defined as "image", but what I'm failing to grasp here is how to save these files to my local directory.
Any help on this matter would be greatly appreciated!
Thanks,
tvick47
The following code would write a UIImage in the Application Documents directory under the filename 'filename.jpg'
var image = .... // However you create/get a UIImage
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let destinationPath = documentsPath.stringByAppendingPathComponent("filename.jpg")
UIImageJPEGRepresentation(image,1.0).writeToFile(destinationPath, atomically: true)
这篇关于Swift - 将图像从URL写入本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!