pro*_*skz 4 image ios nsurlsession swift
我有7个车对象,我需要下载汽车图像并将其设置为Car对象.
下载完成后我在didFinishDownloadingToURL函数中检索图像,我有两个选择:1.直接从临时文件加载图像2.将文件移动到另一个位置并将其保存在目录中(例如).
问题是我不知道如何将这个后退图像设置为相应的对象!如果我使用第一个选项,我怎样才能找到好的和相应的对象来设置图像?
或者,如果我使用seconde选项:将文件保存到目录,如何找到相应的图像文件并将其设置为相应的对象?
实际上我使用NSURLSession从不同的网址下载图像.为此,我有管理URLSession回调的DownloadSessionDelegate.在这里你可以找到我的课程:
typealias CompleteHandlerBlock = () -> ()
class DownloadSessionDelegate: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate {
var handlerQueue : [String : CompleteHandlerBlock]!
class var sharedInstance:DownloadSessionDelegate {
struct Static {
static var instance :DownloadSessionDelegate?
static var token : dispatch_once_t = 0 ;
}
dispatch_once(&Static.token) {
Static.instance = DownloadSessionDelegate();
Static.instance!.handlerQueue = [String : CompleteHandlerBlock]();
}
return Static.instance!
}
// Session delegate
func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) {
println("session error: \(error?.localizedDescription).")
}
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust))
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
println("session \(session) has finished the download task \(downloadTask) of URL \(location).")
var fileHandle:NSFileHandle = NSFileHandle(forReadingAtPath: location.path!)!
var data:NSData = fileHandle.readDataToEndOfFile()
var image:UIImage = UIImage(data: data)! }
Run Code Online (Sandbox Code Playgroud)
我还把handleEventsForBackgroundURLSession int AppDelegate放进去,一切都很好.
我有一个也是唯一一个类:UIImageUtils,我有函数调用DownloadImage.每个不同的类都会调用这个功能来下载他们的图像.在这里你找到这个功能:
// Download image
static func downloadImageFromUrl(urlImage:NSString, writeToDevice: Bool, storeName:NSString, object: AnyObject) {
var configuration = NSURLSessionConfiguration.backgroundSessionConfiguration(SessionProperties.identifier);
var backgroundSession = NSURLSession(configuration: configuration, delegate: DownloadSessionDelegate.sharedInstance, delegateQueue: nil);
var url = NSURLRequest(URL: NSURL(string: ConfigurationManager.host + urlImage)!);
var downloadTask = backgroundSession.downloadTaskWithRequest(url);
downloadTask.resume();
}
Run Code Online (Sandbox Code Playgroud)
非常感谢你的帮助.
而不是使用委托的所有麻烦.您可以使用该downloadTaskWithRequest方法的完成块.我修改了downloadImageFromUrl函数来获取一个块.可以从汽车对象调用此函数,以便在该实例中获得回调.
func downloadImageFromUrl(urlImage:NSString, writeToDevice: Bool, storeName:NSString, object: AnyObject, onfinished:(UIImage) -> ()) {
var configuration = NSURLSessionConfiguration.backgroundSessionConfiguration(SessionProperties.identifier);
var backgroundSession = NSURLSession(configuration: configuration, delegate: DownloadSessionDelegate.sharedInstance, delegateQueue: nil);
var url = NSURLRequest(URL: NSURL(string: ConfigurationManager.host + urlImage)!);
var downloadTask = backgroundSession.dataTaskWithRequest(url, completionHandler: {
data,response,error in
if error == nil
{
if let image = UIImage(data: data)
{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
onfinished(image)
})
}
}
})
downloadTask.resume();
}
Run Code Online (Sandbox Code Playgroud)
在汽车类里面
class Car
{
func downloadImage
{
YourDelegate.downloadImageFromUrl(self.imageUrl, writeToDevice: true, storeName: "store", object: nil, onfinished: { downloadedImage in
self.image = downloadedImage
})
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3843 次 |
| 最近记录: |