2015-04-28 85 views
0

我試圖通過它們的URL加載圖像並將它們按照的順序存儲在NSMutableArray 中。如果我不關心按順序存儲圖像,我的當前代碼正常工作,但是它存儲的順序不是這樣。它目前根據異步請求完成的速度將圖像存儲在articleImage數組中。我試圖玩弄insertObject:AtIndex,但無法取得任何工作。爲了澄清,我試圖存儲圖像(以有序的方式)的NSMutableArray是articleImage如何將我的異步url映像加載到NSMutableArray中?

這裏是我的viewDidLoad中的一些代碼:

dispatch_async(dispatch_get_main_queue(), ^{ 

        if(articleInfoJSONArray.count > 0) 
        { 
         for(int i=0; i<articleInfoJSONArray.count; i++) 
         { 
          [issueID addObject:[[articleInfoJSONArray objectAtIndex:i] objectForKey:@"issueID"]]; 
          [articleID addObject:[[articleInfoJSONArray objectAtIndex:i] objectForKey:@"articleID"]]; 


          NSString *imageLink = [[articleInfoJSONArray objectAtIndex:i] objectForKey:@"articleImage"]; 

          dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 

          dispatch_async(queue, ^{ 

           NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageLink]]; 
           UIImage *image = [UIImage imageWithData:data]; 

           dispatch_async(dispatch_get_main_queue(), ^{ 

            [articleImage addObject:image]; 
            if(articleImage.count == articleInfoJSONArray.count) 
             [self imagesLoaded]; 
           }); 
          }); 
         } 
        }       
       }); 

這裏是我的imagesLoaded:

- (void)imagesLoaded 
{ 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; 
    ViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"MainView"]; 
    [self presentViewController:vc animated:NO completion:nil]; 

} 
+0

不看代碼的話「異步」和「秩序「似乎相互排斥。 – trojanfoe

+0

希望情況並非如此:( –

+0

再一次,沒有看代碼,我可以想到2個解決方案:1.一個後臺線程按順序加載圖像2.將索引號與每個作業相關聯並使用它來存儲圖片在他們的陣列中,1更好,因爲它更簡單,並且可能運行得更快 – trojanfoe

回答

0

一種方法我做的圖像下載是NSOperationQueue和的NSOperation。你可以在你的頭文件中定義一個NSOperationQueue:

@property (strong, nonatomic) NSOperationQueue *sequentialOperationQueue; 

在您的實現做:

self.sequentialOperationQueue = [[NSOperationQueue alloc] init]; 
self.sequentialOperationQueue.maxConcurrentOperationCount = 1; 

那麼你可以添加:

for (NSDictionary *imageDict in imagesToFetch) { 
    ImageDownloadOperation *imgDownloadOperation = [[ImageDownloadOperation alloc] initWithImageLocationDict:imageDict]; 
    [self.sequentialOperationQueue addOperation:imgDownloadOperation]; 
} 

LogoDownloadOperation是的NSOperation的子類。這樣你總是隻有一個活動下載並按照你想要的順序處理它們。有關NSOperation的詳細信息,請查看apple文檔。

中提取我的確在ImageDownloadOperation:

- (void)start { 
    NSURL *imageUrl = [NSURL URLWithString:self.imageDict[@"imageUrl"]]; 

    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig]; 

    NSURLSessionDownloadTask *downloadPhotoTask = [session 
               downloadTaskWithURL:imageUrl 
               completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { 

                if (error) { 
                 self.sessionTask = nil; 


                 [self done]; 


                 return; 
                } 




                NSData *imageData = [NSData dataWithContentsOfURL:location]; 
                NSBlockOperation *saveImageBlockOperation = [NSBlockOperation blockOperationWithBlock:^{ 
                 [SharedAppDelegate.entityManager saveImage:imageData 
                          imageDict:self.imageDict 
                        inManagedObjectContext:SharedAppDelegate.managedObjectContext]; 
                }]; 
                saveImageBlockOperation.qualityOfService = NSQualityOfServiceBackground; 
                [[NSOperationQueue mainQueue] addOperation:saveImageBlockOperation]; 


                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
                self.sessionTask = nil; 
                [self done]; 
               }]; 


    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
} 

正如你所看到的,我通過我的CoreData的AppDelegate存儲爲imageData。而不是我的方式,你可以給ImageDownloadOperation一個指向你的NSMutableArray的指針,然後你可以將數據直接存儲在你的數組中。

0

你可以做的[UIImage new]數組那麼一旦任務完成 更換空的圖像images[i] = newImage

編輯

NSMutableArray *imageArray = [NSMutableArray new]; 

for (int i=0; i<articleInfoJSONArray.count; i++) { 
    [imageArray addObject:[UIImage new]]; 
} 

for (int i=0; i<articleInfoJSONArray.count; i++) { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     //download image 
     imageArray[i] = downloadedImage; 
    }); 
} 
+0

我不遵循:( –

+0

我編輯了我的帖子,更詳細 – Halpo