2013-04-05 2 views
1

이 코드는 기본적으로 서버에서 데이터를 가져 와서 이미지를 만든 다음보기에 추가합니다.이미지를 한 번에 불러 오기

그러나 한 번에 하나씩보기를 업데이트하고 싶습니다.

지금은 모든 이미지가로드 된 후 즉시 모든 이미지로보기가 업데이트됩니다.

새 이미지를 업로드 할 때마다보기를 업데이트하고 싶습니다.이 작업은 addImage 메서드에서 수행해야하지만 대신 모든 이미지로드를 기다리는 중입니다.

이 문제를 해결하기 위해 dispatch_async를 사용해 보았지만 작동하지 않았습니다.

한 번에 하나씩 이미지를로드하려면 어떻게해야합니까?

AFJSONRequestOperation *operation = 
     [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
                 success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     //some code not displayed here that parses the JSON 
     for (NSDictionary *image in images){ 
      if ([[image objectForKey:@"ContentType"] isEqualToString:@"image/png"]){ 
       NSString *imgLink = [[image objectForKey:@"MediaUrl"] JSONStringWithOptions:JKSerializeOptionPretty includeQuotes:NO error:nil]; 
       NSURL *url = [NSURL URLWithString:path]; 
       NSData *data = [NSData dataWithContentsOfURL:url]; 
       UIImage *img = [[UIImage alloc] initWithData:data cache:NO] 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        [self addImage:img]; 
        //This method adds the image to the view 
       }); 
      } 
     } 
    } 

업데이트 :이 이미지는 당신이 게으른로드의 형태를 찾고 있습니다 생각 UIView의

+0

시도 [자기 setNeedsDisplay]; – Shmidt

+0

아니, 그 효과, 같은 결과가 없었다. 나는 AFNetworking이 약간의 간섭을 일으키는 것 같은 느낌이 들지만 처음 iOS 개발자로서 무엇이 문제인지 확신 할 수 없다. 그래도 고마워! :) @Shmidt – GangstaGraham

+0

dispatch_sync를 dispatch_sync로 바꾸어보세요. – Shmidt

답변

0

에 파단로 추가하는있는 tableview 없습니다. 이 예제를보십시오 : 하나에 의해 각각의 이미지 하나를 기다려야 할 수 있도록 Apple Lazy Loading example

+0

질문이 업데이트되었습니다. 이것은 테이블 뷰가 아니며, 여전히이 상황에 적용되는 것이 있는지 확인하기 위해 코드를 점검 할 것입니다. – GangstaGraham

+0

개념은 여전히 ​​동일해야합니다. – iOSGuru248

+0

체크 아웃.나는 실제로 그것이 말한 것과 비슷한 것을하고 있으며 불행히도 나를 위해 일하지 않습니다. – GangstaGraham

0
[NSData dataWithContentsOfURL:url]; 

가 동기화 작업입니다, URL을 기반으로 사진의 적절한 지연로드 구현은 당신이 할 수있는 그 동안 최적의 솔루션이며, 당신은 당신이 당신의 자신의 동시 큐를 만들 것을 권장이 용액 A를 사용하기보다는 global_queue을 사용하려면이

AFJSONRequestOperation *operation = 
[AFJSONRequestOperation JSONRequestOperationWithRequest:request 
               success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
                //some code not displayed here that parses the JSON 
                for (NSDictionary *image in images){ 
                 if ([[image objectForKey:@"ContentType"] isEqualToString:@"image/png"]){ 
                  NSString *imgLink = [[image objectForKey:@"MediaUrl"] JSONStringWithOptions:JKSerializeOptionPretty includeQuotes:NO error:nil]; 
                  NSURL *url = [NSURL URLWithString:path]; 

             dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
                   NSData *data = [NSData dataWithContentsOfURL:url]; 
                   UIImage *img = [[UIImage alloc] initWithData:data cache:NO] 
                   dispatch_async(dispatch_get_main_queue(), ^{ 
                    [self addImage:img]; 
                    //This method adds the image to the view 
                   }); 
                  }) 

                 } 
                } 
               } 

처럼 뭔가를 시도

+0

이 방법을 사용하면 처음 두 이미지가로드되고 나머지 이미지가 한꺼번에로드되므로 어떤 이유로 든 한 번에 하나씩로드되지 않습니다. – GangstaGraham

관련 문제