我有一个奇怪的问题。要求是从一个网址刷卡下载图像,并在图像视图中显示它。它工作得很好,但我收到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];
}
此下载方式1,在一个实时图像并传送到的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;
//DownloadImage.m
NSUrlCo的-(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;
}
}
其他代表nnections实现,但我不把它放在这里。 一旦这个图像被返回,我将此图像设置为滚动视图并显示它并从滚动视图中删除以前的图像。
更多信息: -
只是为了验证我注释掉设置图像滚动型和刚刚下载的每一个刷卡的图像,但它仍然崩溃各地30幅
令人惊讶的,我使用同一个类DownloadImage。 h和.m在同一工作中的其他地方下载图像,即使使用500images也可以工作。
我在iPod Touch上测试和我检查所使用的内存保持12-14mb之间(不可超过本)
请帮我家伙,让我知道,如果你需要更多的细节。
你存储的图像?或只是在用户刷卡时显示它们?如果是第二个,请查看asyncImageView,它是一个ImageView替代品,可以异步显示图像并在需要时缓存它们。 – jcesarmobile 2013-03-08 09:00:18
如果您使用ARC,则不应使用NSAutoReleasePool。而是使用@autoreleasepool。 – occulus 2013-03-08 09:02:19
欲了解更多信息,请参阅http://stackoverflow.com/questions/9086913/objective-c-why-is-autorelease-autoreleasepool-still-needed-with-arc – occulus 2013-03-08 09:02:44