My code take a lot time to load images from gallery. I store url of each image in database and then load them from gallery using AssetsLibrary framework. But i notice that it take more memory and so that i receive memory warning. after some time app is crash down. Here i put my code.
-(void) nextImageLoad
{
// ALAssetsLibrary* self. assetslibrary=[[ALAssetsLibrary alloc] init];
self.myScrollView.pagingEnabled=YES;
NSLog(@"current :- %d, loaded_photo :- %d, [imageData count] :- %d",current, loaded_photo, [imageData count]);
if(current>loaded_photo && current < [imageData count])
{
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
self.rep = [myasset defaultRepresentation];
CGImageRef iref = [self.rep fullResolutionImage];
if (iref)
{
UIImageView *temp = [[UIImageView alloc] initWithFrame:CGRectMake(self.x, y, 300, 300)];
temp.image=[UIImage imageWithCGImage:iref];
[self.myScrollView addSubview:temp];
[temp release];
[myScrollView setContentSize:CGSizeMake(self.x+600, 410)];
[myScrollView setAutoresizesSubviews:YES];
}
else
{
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
};
[self.assetslibrary assetForURL:[NSURL URLWithString:[[self.imageData objectAtIndex:current] valueForKey:@"photo_url"] ]
resultBlock:resultblock
failureBlock:failureblock];
self.x=self.x+320;
loaded_photo= loaded_photo+1;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
lastContentOffset = scrollView.contentOffset.x;
}
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (lastContentOffset > scrollView.contentOffset.x)
{
NSLog(@"left");
if(current>0)
current=current-1;
}
else if (lastContentOffset < scrollView.contentOffset.x)
{
NSLog(@"right");
current=current+1;
[self nextImageLoad];
}
lastContentOffset = scrollView.contentOffset.x;
}
Hard to know if this is the problem, but try creating an autorelease pool inside all your blocks:
Also remember if you return early inside the block, you need to release the pool object before your return statement.