我有一个iOS项目,我在自己的课程中使用ARC,但在其他库如ASIHTTPRequest
中关闭了ARC。ASIHTTPRequest内存泄漏
-(void)buildPhotoView {
self.photoLibView.hidden = NO;
NSString *assetPathStr = [self.cellData objectForKey:@"AssetThumbPath"];
// get the thumbnail image of the ocPHOTOALBUM from the server and populate the UIImageViews
NSURL *imageURL = [NSURL URLWithString:assetPathStr];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:imageURL];
__unsafe_unretained ASIHTTPRequest *weakRequest = request;
[weakRequest setCompletionBlock:^{
// put image into imageView when request complete
NSData *responseData = [weakRequest responseData];
UIImage *photoAlbumImage = [[UIImage alloc] initWithData:responseData];
self.photo1ImageView.image = photoAlbumImage;
}];
[weakRequest setFailedBlock:^{
NSError *error = [request error];
NSLog(@"error geting file: %@", error);
}];
[weakRequest startAsynchronous];
}
我修改从ASIHTTPRequest
示例代码页的样例代码:
我使用下面的代码来从一个Web服务器的图像获取巨大的内存泄漏消除Xcode中的编译器警告。
我该如何摆脱这些内存泄漏?我只是在使用块时才会得到它们。
使用autoreleased对象实例化'photoAlbumImage'是否会减少泄漏的大小?即'photoAlbumImage = [UIImage imageWithData:responseData];' – FluffulousChimp
使用自动引用计数时不能使用autoRelease。当我使用ASIHTTPRequest而没有使用块时,我没有得到内存泄漏,但我需要这种情况,因为我正在对图像执行多个请求,每个请求都会进入tableCell中的不同UIImageView。使用块,我可以在请求中包含一个完成块,在请求完成时将图像放入正确的UIImageView。 – Alpinista