ios – 使用NSURLSession下载后,将图像设置为对象

ios – 使用NSURLSession下载后,将图像设置为对象,第1张

概述我有7个车对象,我需要下载汽车图像并将其设置为Car对象. 下载完成后我在didFinishDownloadingToURL函数中检索图像,我有两个选择:1.直接从临时文件加载图像2.将文件移动到另一个位置并将其保存在目录中(例如). 问题是我不知道如何将这个后退图像设置为相应的对象! 如果我使用第一个选项,我怎样才能找到好的和相应的对象来设置图像? 或者,如果我使用seconde选项:将文件保存 我有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 delegatefunc 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)! }

我还把handleEventsForBackgroundURLSession int AppDelegate,一切都很好.

我有一个也是唯一一个类:UIImageUtils,我有函数调用DownloadImage.每个不同的类都会调用这个功能来下载他们的图像.在这里你找到这个功能:

// Download imagestatic 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();}

非常感谢你的帮助.

解决方法 而不是使用委托的所有麻烦.您可以使用downloadTaskWithRequest方法的完成块.
我修改了downloadImageFromUrl函数来获取一个块.可以从汽车对象调用此函数,以便在该实例中获得回调.

func downloadImageFromUrl(urlimage:Nsstring,object: AnyObject,onfinished:(UIImage) -> ()) {    var configuration = NSURLSessionConfiguration.backgroundSessionConfiguration(SessionPropertIEs.IDentifIEr);    var backgroundSession = NSURLSession(configuration: configuration,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();}

在汽车类里面

class Car{    func downloadImage    {        YourDelegate.downloadImageFromUrl(self.imageUrl,writetoDevice: true,storename: "store",object: nil,onfinished: { downloadedImage in            self.image = downloadedImage        })    }}
总结

以上是内存溢出为你收集整理的ios – 使用NSURLSession下载后,将图像设置为对象全部内容,希望文章能够帮你解决ios – 使用NSURLSession下载后,将图像设置为对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1060913.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存