2013-03-08 2 views
1

나는 이상한 문제가 있습니다. 요구 사항은 스 와이프 URL에서 이미지를 다운로드하여 이미지보기에 표시하는 것입니다. 괜찮습니다.하지만 30 개 이미지와 몇 번의 스 와이프 앱 충돌 후 메모리 경고가 표시됩니다.이미지를 다운로드하는 동안 iphone 메모리 문제가 발생했습니다.

구현은 꽤 간단하지만 이미이 문제를 파악하는 데 거의 2 일을 보냈습니다. 각 스 와이프 는 내가하는 방법을 호출 오전 : - 한 번에

-(void)callDownloadImageAPI{ 

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]; 
    [self loadIndicator:@"Please Wait.!!" :@"Please be patient while we are downloading image for you"]; 
    @try{ 
     DownloadImage *downLoadImge =[[DownloadImage alloc] init]; 
     downLoadImge.delegate=self; 
     [downLoadImge getImage:[self.allUrlArray objectAtIndex:self.initialImageViewCounter]]; 
    } 
    @catch (NSException *e) { 
     NSLog(@"callDownloadImageAPI exception %@",[e description]); 
     [HUD hide:YES];   
    } 
    [pool release]; 
} 

이 방법 다운로드 한 이미지와 그 대리인에게있는 UIImage를 보내

// DownloadImage.h의 구현 및하는 .m

@protocol DownloadImageDelegate 
    @required 
    - (void)messageFormServerWithImage:(UIImage*)imageFromSever; 
    - (void)gettingImageFailed :(NSString*)errorDesc; 
    @end 



     @interface DownloadImage : NSObject 

     @property(strong) NSURLConnection*      connection; 
     @property(weak) id<DownloadImageDelegate>    delegate; 
     @property(strong) NSMutableData*       data; 

     -(void)getImage:(NSString *)imageUrl; 
NSUrlCo의

//DownloadImage.m

-(void)getImage:(NSString *)imageUrl{ 
    @autoreleasepool { 

     [[NSURLCache sharedURLCache] removeAllCachedResponses]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60]; 
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    } 
} 

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection { 
    @autoreleasepool { 

    NSLog(@"connectionDidFinishLoading"); 
    self.connection=nil; 
    if(self.data == nil) { 
     return; 
    } 

// NSString* jsonStr = [[NSString alloc] initWithData:self.data encoding:NSASCIIStringEncoding]; 
    UIImage *img=[UIImage imageWithData:self.data]; 
// NSArray *messages_json = [parser objectWithString:jsonStr error:nil]; 
    [self.delegate messageFormServerWithImage:img]; 
    self.data = nil; 
    img= nil; 
    } 
} 

다른 대표 연결은 구현되었지만 여기에 넣지는 않습니다. 이 이미지가 반환되면이 이미지를 scrollview로 설정하고 표시하고 scrollview에서 이전 이미지를 삭제합니다.

추가 정보 : -

그냥 내가있는 ScrollView하는 이미지를 설정 주석 그냥 와이프 이미지를 다운로드하지만 여전히 놀랍게도 내가 같은 클래스 DownloadImage를 사용하고 약 30 개 이미지

충돌 확인합니다. h와 .m을 사용하면 동일한 작업의 다른 위치에서 이미지를 다운로드 할 수 있으며 500 매 이미지로도 멋지게 작동합니다.

나는 아이팟 터치에서 테스트 그리고 난,

이 사람을 저를 도와주세요 (이 초과하지 않습니다) 당신은 자세한 내용이 필요하면 알려 주시기 사용되는 메모리가 12-14메가바이트 사이에 남아 확인.

+0

이미지를 저장 하시겠습니까? 또는 사용자가 스 와이프 할 때 표시 할 수 있습니까? 두 번째 경우 asyncImageView를 살펴보십시오. 이미지를 비동기 적으로 표시하고 원하는 경우 캐시하는 ImageView 대체품입니다. – jcesarmobile

+0

ARC를 사용하는 경우 NSAutoReleasePool을 사용하지 않아야합니다. 대신 @autoreleasepool을 사용하십시오. – occulus

+0

자세한 내용은 http://stackoverflow.com/questions/9086913/objective-c-why-is-autorelease-autoreleasepool-still-needed-with-arc – occulus

답변

1

모든 이미지가 가상 메모리에 저장되기 때문에 충돌이 발생하므로 실제로 캐싱하고 메모리에 다시로드해야합니다.

캐시 된 또는 필요하지 않은 이미지 객체도 nil로 설정해보십시오.

클래스에서 didReceiveMemoryWarning 메서드를 사용하고이 메서드를 호출 할 때 메모리에서 일부 이미지를 릴리스하는 것이 좋습니다.

+0

답변 주셔서 감사합니다 shoughton123. 가상 메모리에있을 수도 있습니다. 그러나 제공된 답변에 대해서는 의심의 여지가 있습니다.심지어 이미지를 캐싱하기 위해 처음으로 atleast를 다운로드해야 할 것입니다. 바로 캐싱하고 캐시에서로드하는 것이 문제가 지속됩니다. 스 와이프 자체에서 보이지 않는 다른 모든 이미지를 삭제하므로 didReceiveMemoryWarning에 도달해서는 안됩니다. –

관련 문제