首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS应用程序中下载多幅图像

在iOS应用程序中下载多幅图像
EN

Stack Overflow用户
提问于 2013-02-22 12:28:34
回答 4查看 6.4K关注 0票数 1

我想下载多个图片从URL到我的设备在iOS应用程序。

代码语言:javascript
复制
//
//  ImageDownload.m
//

#import "ImageDownload.h"

@implementation ImageDownload {
    int *position;
    NSArray *downloableImages;
}

- (void)start:(NSArray *)images delegate:(id)delegate
{
    position = 0;
    downloableImages = images;

    NSUInteger *count = ((NSUInteger *)[downloableImages count]);
    NSLog(@"%d", count);

    [self startDownload];
}

- (void)startDownload
{
    NSUInteger *imageDataCount;

    NSArray *image;
    NSString *filename;
    NSString *fileurl;

    NSURLRequest *imageUrlRequest;

    image = [downloableImages objectAtIndex:position];

    NSLog(@"%d", position);

    NSArray *imageData = [image valueForKey:@"image"];
    imageDataCount = ((NSUInteger *)[imageData count]);

    if (imageDataCount > 0) {
        filename = [imageData objectAtIndex:0];
        fileurl = [imageData objectAtIndex:1];

        NSLog(@"%@", fileurl);

        imageUrlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:fileurl]];
        NSURLConnection *imageUrlConnection = [[NSURLConnection alloc] initWithRequest:imageUrlRequest delegate:self startImmediately:TRUE];
    } else {
        NSUInteger *count = ((NSUInteger *)[downloableImages count]);

        if (((NSUInteger *)position) < ((NSUInteger *)count - 1)) {
            position = position + 1;

            [self startDownload];
        }
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"finish image...");
    NSUInteger *count = ((NSUInteger *)[downloableImages count]);

    if (((NSUInteger *)position) < ((NSUInteger *)count - 1)) {
        position = position + 1;

        [self startDownload];
    }
}

@end

现在..。我只检查下载的位置和当前的URL,存在27个文件供下载.但下载不是一个接一个..。检查此输出:

职位:0 http.fic海洛因00.jpg 完成下载位置:4 http.fic海洛因04.jpg 完成下载位置:8 http.fic海洛因08.jpg 完成下载

EN

回答 4

Stack Overflow用户

发布于 2013-02-22 12:51:20

在您的NSOperationQueue *operationQueue;文件中创建一个ivar .h,并在您的.m文件中插入它:

代码语言:javascript
复制
operationQueue = [[NSOperationQueue alloc] init];
operationQueue.maxConcurrentOperationCount = 1;

要下载图像,创建一个获取图像URL并为每个图像调用它的方法:

代码语言:javascript
复制
-(void)downloadImageAtURL:(NSURL *)imageURL {

    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imageURL] queue:operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        // Do something with your images here...
    };
}
票数 3
EN

Stack Overflow用户

发布于 2013-02-22 12:48:08

你可以试试这段代码。它百分之百地为我工作

你可以在这里得到更多的参考资料

代码语言:javascript
复制
-(IBAction)startdownload
{
for (int i=0; i<[downloadarray count]; i++)   //download array have url links
{ 
NSURL *URL = [NSURL URLWithString:[downloadarray objectAtIndex:i]];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:URL];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue  completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
  if([data length] > 0 && [[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"])
{
 //make your image here from data.
 UIImage *imag = [[UIImage alloc] initWithData:[NSData dataWithData:data]];
 NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
 NSString *docDir = [array objectAtIndex:0];
 NSString *imgstr=[NSString stringWithFormat:@"%d",i];
 NSString *pngfilepath = [NSString stringWithFormat:@"%@sample%@.png",docDir,imgstr];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(imag)];
 [data1 writeToFile:pngfilepath atomically:YES];
  }
 else if ([data length] == 0 && [[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"])
{
  NSLog(@"No Data!");
}
  else if (![[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"]){
 NSLog(@"Error = %@", error);
 }
}];

 }

 -(IBAction)viewfile
 {
 NSMutableArray *arr=[[NSMutableArray alloc]init];

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *pngfilepath = [NSString stringWithFormat:@"%@",documentsDirectory];
 NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:pngfilepath error:&error];
 for (int i=0; i<[filePathsArray count]; i++){
 NSString *pngfilepath = [NSString stringWithFormat:@"%@%@",documentsDirectory,  [filePathsArray objectAtIndex:i]];
 [arr addObject:[UIImage imageWithContentsOfFile:pngfilepath]];
 }
myimageview = [[UIImageView alloc] initWithImage:[arr objectAtIndex:0]]; //Here myimageview is UIImageView

 }

希望这会有帮助!

票数 2
EN

Stack Overflow用户

发布于 2013-02-22 12:42:56

围绕NSOperationQueue工作,它允许您控制并发运行的操作数量。

创建:

代码语言:javascript
复制
NSOperationQueue *queue = [NSOperationQueue new];
queue.maxConcurrentOperationCount = 1;

然后添加操作:

代码语言:javascript
复制
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
    ... here is you code with converting image to data and addition it to NSURLConnection
}];
[operation setCompletionBlock:^{
    ....something that you want to do, after operation completes
}];
[queue addOperation:operation];

更多关于NSOperation:参考文献/occ/cl/NSOperation的信息

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15024261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档