从URL iPhone非同步下载 [英] Asynch download from URL iPhone

查看:128
本文介绍了从URL iPhone非同步下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想问一个很好的教程,显示我如何从URL中使用目标C文件下载到本地电话存储
我做了以下的同步,但我想使它成为一个同步

Hi I want to ask for a good tutorial that shows me how to download a file from URL to local phone storage using objective C I have done the following its sync but i want to make it a synch

  NSString* docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString* fileToDownload = @"data1.plist";
NSString* hostURLString = @"http://localhost/test";
hostURLString = [hostURLString stringByAppendingPathComponent: fileToDownload];
NSURL* pListURL = [NSURL URLWithString: hostURLString];
NSData* pListData = [NSData dataWithContentsOfURL: pListURL];
NSString* filePath = [docsDir stringByAppendingPathComponent: fileToDownload];
[pListData writeToFile: filePath atomically: NO];


NSString* Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *DataPath = [Path stringByAppendingPathComponent:@"data1.plist"];

任何建议

推荐答案

下面是我的code为异步下载图像和数据。你可以用它玩你的目标。

Below is my code for async downloading images and data. You can play with it for your aims.

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        NSLog(@"Screen %@ - pauseBannerFileImage download starts", self.name);          
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:newUrlForImage]]];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:newUrlForImage]];
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"!-Screen %@-!pauseBannerFileImage downloaded", self.name);
            self.pauseBannerFileImage = image;
        });
    });

这篇关于从URL iPhone非同步下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆