簡體   English   中英

iPhone中ImagePickerView中的內存泄漏

[英]Memory leak in ImagePickerView in iPhone

我正在使用Scrollviewer加載圖像。 我從媒體庫中獲取圖像並將其保存到本地文件夾中。如果我選​​擇並添加的圖像超過5張,則應用程序獲得內存警告級別2應用程序崩潰。

這是從數據庫獲取照片的代碼:

-(NSMutableArray *)GetPhotos:(int)folderId {NSString * query = [[[[NSString alloc] initWithFormat:@“ SELECT * FROM Photo WHERE FolderID =?”]自動釋放];

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; 

}

從圖像選擇器視圖控制器中選擇圖像:

實用標記UIImagePickerController委托

  • (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);

}

這是將圖像加載到scrollviewer中的方法。 此方法只會導致內存泄漏

-(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;

}

自定義按鈕類別:

@class照片; @interface PhotoView:UIButton {

Photo *photo;

} @property(非原子,保留)圖片* photo;

@結束

我沒有正確釋放對象的地方? 內存泄漏在哪里? 當我選擇圖像和loadimages時會發生此內存問題。

內存警告不是內存泄漏,而是表明內存使用率更高。 使用工具來查看內存增長發生的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM