2014-03-27 2 views
0

서버에서 수천 장의 사진 (최대 350KB)을 다운로드하려고하는데 "Memory Presure"라는 경고 메시지가 나타납니다. 기본적으로메모리 부족으로 많은 이미지 다운로드

나는 모든 이미지의 이름과 배열을 가지고 루프는 다음과 같이 하나 하나를 가지고 않습니다

for (int x=0; x<unique.count; x++) { 

    NSURL *ImageLink = [NSURL URLWithString:[NSString stringWithFormat:@"http://urltoimagesfolder.com/", [unique objectAtIndex:x]]]; 
    NSData *data = [NSData dataWithContentsOfURL:ImageLink]; 
    UIImage *img = [[UIImage alloc] initWithData:data]; 

    if (data.length !=0) { 

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[unique objectAtIndex:x]]; //add our image to the path 

    [UIImageJPEGRepresentation(img, 1.0) writeToFile:fullPath atomically:YES]; 

    //[self saveImage:img :NombreFoto]; 
    //[self Miniatura:img :[NSString stringWithFormat:@"mini-%@", [unique objectAtIndex:x]]]; 
    } 

    data = nil; 
    img = nil; 


} 

질문 :와 응용 프로그램 충돌없이 모든 이미지를 다운로드 할 수있는 방법 기억력?

+0

무엇이 당신의 질문입니까? – rocky

+0

편집 메모리 압박으로 앱이 충돌하지 않고 모든 이미지를 어떻게 다운로드 할 수 있습니까? –

+0

음 ... 작성한 후에 img을 출시 하시겠습니까? – rocky

답변

0

UIImageJPEGRepresentation()은 메모리 오버플로를 유발할 수 있습니다. 하지만이 기능을 사용할 필요가없는 경우 수신 된 데이터가 이미지인지 확인하고 메시지를 writeToFile:에서 data 개체로 디스크에 직접 쓸 수 있습니다.

당신은 당신이 이런 식으로 코드를 수정할 수 있습니다

for (int x=0; x<unique.count; x++) { 
    NSURL *ImageLink = [NSURL URLWithString:[NSString stringWithFormat:@"http://urltoimagesfolder.com/", [unique objectAtIndex:x]]]; 
    NSData *data = [NSData dataWithContentsOfURL:ImageLink]; 
    if (data.length !=0) { 

     UIImage *img = [[UIImage alloc] initWithData:data]; 
     if (img) { 
      NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[unique objectAtIndex:x]]; //add our image to the path 
      [data writeToFile:fullPath atomically:YES]; 
     } 
     img = nil; 

    } 
    data = nil; 
} 

그러나이 최적의 솔루션이 아닙니다. -dataWithContentsOfURL:은 동기 메서드이며 파일을 다운로드하는 동안 주 스레드의 실행을 중지합니다. 결과적으로 다운로드 중에 UI가 중단됩니다. UI를 중단시키지 않으려면 비동기 url 요청을 사용할 수 있습니다.

NSURLConnection 클래스의 -sendAsynchronousRequest:queue:completionHandler: 메서드를 참조하십시오. 또는 앱이 iOS 7 전용 인 경우 -dataTaskWithURL:completionHandler:을 참조하십시오.

관련 문제