Hi there i am working on an application which loads images from web for filling up the content view. i am using "MGBox2" for loading up the views into the sections and i am using a modified version of "Photobox" class in the mgbox2 example.
My problem is, when i load images from the web, the imageview objects retain in the memory even i dismiss/removefromsuperview the viewcontroller and views that contains them.
When i look at to the memory allocations i see that i have tons of "malloc 9.00kb" objects with retain count 1 which points to
UIImage* image = [UIImage imageWithData:weakData];
line. Heres my whole code for loading image and put it into photobox:
My code looks like a mess, becauase i am constantly trying new things, and i know i have some problems with understanding how arc works.. tried to set the data to __weak but as you can see, still no luck..
also note that if an image url contains "publisher" word in it, i am saving it to the documents directory for caching on disk.
- (void)loadPhotoFromURL:(NSString*)photoUrl; {
bool hasLoaded = NO;
for(id view in self.subviews){
if([[view class] isSubclassOfClass:[UIImageView class]])
hasLoaded = YES;
}
if(!hasLoaded){
id fullPath = photoUrl;
bool willCachePhoto = NO;
if([photoUrl rangeOfString:@"publishers"].location!=NSNotFound){
willCachePhoto = YES;
}
NSData* data;
ASIHTTPRequest* request;
if(willCachePhoto)
data = [NewsUtil loadData:[fullPath md5]];
if(!data){
NSURL *url = [NSURL URLWithString:fullPath];
request = [[ASIHTTPRequest alloc]initWithURL:url];
[request startSynchronous];
data = [request responseData];
if(willCachePhoto){
[NewsUtil saveData:data withName:[fullPath md5]];
}
}[request clearDelegatesAndCancel];
// do UI stuff back in UI land
dispatch_async(dispatch_get_main_queue(), ^{
@autoreleasepool {
// ditch the spinner
UIActivityIndicatorView *spinner = self.subviews.lastObject;
[spinner stopAnimating];
[spinner removeFromSuperview];
// failed to get the photo?
if (!data) {
self.alpha = 0.3;
return;
}
__unsafe_unretained NSData* weakData = data;
UIImage* image = [UIImage imageWithData:weakData];
UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageView.image = image;
[self insertSubview:imageView atIndex:0];
imageView.size = self.size;
imageView.alpha = 0;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleHeight;
// fade the image in
[UIView animateWithDuration:0.2 animations:^{
imageView.alpha = 1;
}];
for(id v in self.subviews){
if([[v class]isSubclassOfClass:[UILabel class]]){
UILabel* lbl = (UILabel*)v;
NSLog(@"lbl title: %@",lbl.text);
[self bringSubviewToFront:lbl];
break;
}
}
}//autorelease pool
});
}
}
Thanks for helping!