7

我正在使用UIImagePickerControllerSourceTypeCamera自定义打开相机,cameraOverlayView因此我可以在没有“使用照片”步骤的情况下拍摄多张照片。

这很好用,但保存照片功能存在内存泄漏。通过大量的调试和研究,我将其范围缩小到UIGraphicsGetImageFromCurrentImageContext功能。

这是发生它的代码片段:

UIGraphicsBeginImageContextWithOptions(timestampedImage.frame.size, timestampedImage.opaque, [[UIScreen mainScreen] scale]);
[[timestampedImage layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalTimestampImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我已经搜索了互联网,似乎该UIGraphicsGetImageFromCurrentImageContext()函数(引用自这个 SO 问题“返回一个新的自动释放UIImage并将finalTimestampImageivar 指向它。先前分配UIImage的变量从未真正释放,它的变量只是重新指向其他地方。”

我尝试了很多显然对其他人有用的解决方案:

  • timestampedImage.layer.contents = nil;之后添加UIGraphicsEndImageContext

  • 添加CGContextRef context = UIGraphicsGetCurrentContext();CGContextRelease(context);之后UIGraphicsEndImageContext

  • 将上面的代码片段包装在一个NSAutoreleasePool

  • 将整个saveThisPhoto函数包装在一个NSAutoreleasePool

  • 创建一个NSAutoreleasePool当相机弹出并调用[pool release]whendidReceiveMemoryWarning被调用

  • 调用时关闭相机弹出窗口didReceiveMemoryWarning,希望它会清除池

  • 以上所有可能的组合

Memory Utilized然而,我尝试的一切,当我在设备上反复拍照时,我可以看到上升而不是下降。

有谁知道我如何释放由创建的自动释放对象UIGraphicsGetImageFromCurrentImageContext

或者,是否有另一种方法来制作UIImage一个UIImageView

编辑:

以下是所要求的全部功能。那里添加了很多额外的版本,只是为了确保一切都被清理干净。我已经saveThisPhoto系统地检查并测试了每个代码块的内存泄漏,它只发生在UIGraphicsGetImageFromCurrentImageContext块(上面的片段)运行时。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSLog(@"SAVING PHOTO");

    [self saveThisPhoto:info];

    picker = nil;
    [picker release];

    info = nil;
    [info release];

}

- (void)saveThisPhoto:(NSDictionary *)info {

    // Get photo count for filename so we're not overriding photos

    int photoCount = 0;

    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"photocount"]) {
        photoCount= [[[NSUserDefaults standardUserDefaults] objectForKey:@"photocount"] intValue];
        photoCount++;
    }

    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d", photoCount] forKey:@"photocount"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    // Obtaining saving path

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName = [NSString stringWithFormat:@"ri_%d.jpg", photoCount];
    NSString *fileNameThumb = [NSString stringWithFormat:@"ri_%d_thumb.jpg", photoCount];
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSString *imagePathThumb = [documentsDirectory stringByAppendingPathComponent:fileNameThumb];

    // Extracting image from the picker and saving it

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    // SAVE TO IPAD AND DB

    if ([mediaType isEqualToString:@"public.image"]){

        // Get Image

        UIImage *editedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

        // Figure out image orientation

        CGSize resizedSize;
        CGSize thumbSize;

        if (editedImage.size.height > editedImage.size.width) {
            resizedSize = CGSizeMake(480, 640);
            thumbSize = CGSizeMake(150, 200);
        } else {
            resizedSize = CGSizeMake(640, 480);
            thumbSize = CGSizeMake(150, 113);
        }

        // MAKE NORMAL SIZE IMAGE

        UIImage *editedImageResized = [editedImage resizedImage:resizedSize interpolationQuality:0.8];

        // clean up the one we won't use any more

        editedImage = nil;
        [editedImage release];

        // ADD TIMESTAMP TO IMAGE

        // make the view

        UIImageView *timestampedImage = [[UIImageView alloc] initWithImage:editedImageResized];
        CGRect thisRect = CGRectMake(editedImageResized.size.width - 510, editedImageResized.size.height - 30, 500, 20);

        // clean up

        editedImageResized = nil;
        [editedImageResized release];

        // make the label

        UILabel *timeLabel = [[UILabel alloc] initWithFrame:thisRect];
        timeLabel.textAlignment =  UITextAlignmentRight;
        timeLabel.textColor = [UIColor yellowColor];
        timeLabel.backgroundColor = [UIColor clearColor];
        timeLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(25.0)];
        timeLabel.text = [self getTodaysDateDatabaseFormat];
        [timestampedImage addSubview:timeLabel];

        // clean up what we won't use any more

        timeLabel = nil;
        [timeLabel release];

        // make UIIMage out of the imageview -- MEMORY LEAK LOOKS LIKE IT IS IN THIS BLOCK

        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        UIGraphicsBeginImageContextWithOptions(timestampedImage.frame.size, timestampedImage.opaque, [[UIScreen mainScreen] scale]);
        [[timestampedImage layer] renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *finalTimestampImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        timestampedImage.layer.contents = nil;
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextRelease(context);

        // clean up the one we won't use any more

        timestampedImage = nil;
        [timestampedImage release];

        // SAVE NORMAL SIZE IMAGE TO DOCUMENTS FOLDER

        NSData *webDataResized = UIImageJPEGRepresentation(finalTimestampImage, 1.0); // JPG

        [webDataResized writeToFile:imagePath atomically:YES];

        // clean up the one we won't use any more

        finalTimestampImage = nil;
        [finalTimestampImage release];

        [pool release]; // to get rid of the context image that is stored

        // SAVE TO DATABASE

        [sqlite executeNonQuery:@"INSERT INTO inspection_images (agentid,groupid,inspectionid,areaid,filename,filenamethumb,filepath,orderid,type) VALUES (?, ?, ?, ?, ?, ?, ?, ?,?) ",
         [NSNumber numberWithInt:loggedIn],
         [NSNumber numberWithInt:loggedInGroup],
         myInspectionID,
         [[tableData objectAtIndex:alertDoMe] objectForKey:@"areaid"],
         fileName,
         fileNameThumb,
         documentsDirectory,
         [NSNumber numberWithInt:photoCount],
         [NSNumber numberWithInt:isPCR]
         ];

        // Clean up

        webDataResized = nil;
        [webDataResized release];

    } else {

        NSLog(@">>> IMAGE ***NOT*** SAVED");

    }

    NSLog(@"IMAGE SAVED - COMPLETE");

    info = nil;
    [info release];

}
4

1 回答 1

1

您在释放它们之前将变量设置为 nil,有些已经自动释放。

通常在使用释放时,您应该释放并将它们设置为 nil。

[var release]
var = nil;

但在其中一些你不应该调用释放。

以下是你的罪魁祸首。

    // clean up the one we won't use any more

    timestampedImage = nil;
    [timestampedImage release];

    // SAVE NORMAL SIZE IMAGE TO DOCUMENTS FOLDER
于 2013-11-15T21:35:04.710 回答