I am using the Scrollviewer to load images. I get the images from Media Library and save it into local folder.If i select and add images more than 5, app get memory warning level2 app get crash.
This is code to get the Photos from database:
-(NSMutableArray*) GetPhotos:(int)folderId { NSString *query =[[[NSString alloc] initWithFormat:@"SELECT * FROM Photo WHERE FolderID = ?"] autorelease];
FMDatabase *db = [self.dbUtils sharedDB];
FMResultSet *rs = [db executeQuery:query, [NSNumber numberWithInt:folderId]];
NSMutableArray *results = [[NSMutableArray alloc] init];
while([rs next]) {
Photo *photo = [[Photo alloc] init];
photo.PhotoID = [rs intForColumn:@"PhotoID"];
photo.FolderID = [rs intForColumn:@"FolderID"];
photo.PhotoName = [rs stringForColumn:@"PhotoName"];
photo.UpdatedDate = [rs stringForColumn:@"UpdatedDate"];
photo.ImageData = [rs dataForColumn:@"ImageData"];
photo.Path = [rs stringForColumn:@"Path"];
photo.isPrivacy = [rs boolForColumn:@"isPrivacy"];
[results addObject:photo];
[photo release];
}
[rs close];
return results;
}
Pick the images from Image picker view controller:
pragma mark UIImagePickerController delegate
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissModalViewControllerAnimated:YES];
PrivacyPixAppDelegate *appDelegate = [PrivacyPixAppDelegate appDelegate];
dispatch_queue_t image_queue; image_queue = dispatch_queue_create("com.gordonfontenot.app", NULL);
dispatch_async(image_queue, ^{
dispatch_async(dispatch_get_main_queue(), ^{ UIImage *pickedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; NSString *fileDirectory = [FileUtils documentsDirectoryPath]; fileDirectory = [fileDirectory stringByAppendingFormat:@"/%@/",self.FolderName]; NSString *fileName = [NSString stringWithFormat:@"%@.jpg",[appDelegate newUUID]]; Photo *photo = [[Photo alloc] init]; photo.PhotoName = fileName; photo.Path = fileDirectory; photo.FolderID = self.FolderID; photo.isPrivacy = FALSE; self.btnEdit.hidden = NO; fileDirectory = [fileDirectory stringByAppendingFormat:@"%@",fileName]; NSLog(@"FileName:%@",fileName); NSLog(@"Directory:%@",fileDirectory); NSData *jpegData = UIImageJPEGRepresentation(pickedImage,5.0); [jpegData writeToFile:fileDirectory atomically:NO]; PhotoDAO *dao = [[appDelegate daos] sharedPhotoDAO]; [dao AddPhoto:photo]; [photo release]; NSMutableArray *list = [dao GetPhotos:FolderID]; self.listData = list; [list release]; [self loadForm:self.listData]; });
}); dispatch_release(image_queue);
}
This is method for load images into scrollviewer. This method only problem to memory leak
-(void)loadForm:(NSMutableArray*)list { NSMutableArray *photos = [[NSMutableArray alloc] init];
if([list count] == 0)
self.btnEdit.hidden = YES;
else
self.btnEdit.hidden = NO;
for (int Count = 0; Count < [list count] ; Count ++)
{
Photo *photo = [list objectAtIndex: Count];
PhotoView *photoView = [[PhotoView alloc] initWithFrame: CGRectMake(ThumbnailSizeWidth * (Count % THUMBNAIL_COLS) + PADDING * (Count % THUMBNAIL_COLS) + PADDING,
ThumbnailSizeHeight * (Count / THUMBNAIL_COLS) + PADDING * (Count / THUMBNAIL_COLS) + PADDING + PADDING_TOP,
ThumbnailSizeWidth,
ThumbnailSizeHeight)];
[photoView setPhoto:photo];
[photoView setTag:Count];
photoView.showsTouchWhenHighlighted = YES;
photoView.userInteractionEnabled = YES;
photoView.layer.cornerRadius = 8.0;
if([FileUtils fileExistsAtPath:photo.Path fileName:photo.PhotoName])
{
UIImage *tImage= nil;
if(photo.isPrivacy)
tImage = [UIImage imageNamed:@"locked.png"];
else
{
tImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@%@",photo.Path,photo.PhotoName]];
MyPhoto *photo = [[MyPhoto alloc] initWithImage:tImage];
[tImage release];
[photos addObject:photo];
[photo release];
}
[photoView setImage:tImage forState:UIControlStateNormal];
[photoView addTarget:self action:@selector(FormClicked:) forControlEvents:UIControlEventTouchUpInside];
photoView.frame = CGRectMake(ThumbnailSizeWidth * (Count % THUMBNAIL_COLS) + PADDING * (Count % THUMBNAIL_COLS) + PADDING,
ThumbnailSizeHeight * (Count / THUMBNAIL_COLS) + PADDING * (Count / THUMBNAIL_COLS) + PADDING + PADDING_TOP,
ThumbnailSizeWidth,
ThumbnailSizeHeight);
[scrollViewer addSubview:photoView];
[photoView release];
}
}
if(source)
[source release];
source = [[MyPhotoSource alloc] initWithPhotos:photos];
[photos release];
CGFloat scrollableHeight = ( ThumbnailSizeHeight / THUMBNAIL_COLS) * [list count] + PADDING;
scrollViewer.contentSize = CGSizeMake(320, scrollableHeight + ( ThumbnailSizeWidth * 2) );
scrollViewer.clipsToBounds = YES;
}
Custom Button Class:
@class Photo; @interface PhotoView : UIButton {
Photo *photo;
} @property(nonatomic,retain) Photo *photo;
@end
Where i am not release objects properly? where is the memory leak?. This memory issue is occur when i pick image and loadimages.