UIImagePickerController使用相机源耗尽内存

时间:2010-01-15 13:54:52

标签: ios uiimage uiimagepickercontroller disk

使用UIImagePickerController并将图像保存在磁盘上时出现了严重的性能问题。我无法弄清楚我做错了什么。这是我的代码:

- (void)imagePickerController:(UIImagePickerController *)pick 

didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
    iPixAppDelegate *delegate = (iPixAppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate addPicture:imageData];    
}

addPicture方法创建一个以这种方式初始化的新图片对象:

- (Picture*) initPicture:(NSData*)dat inFolder:(NSString*)pat {
    self.data = dat;
    NSDate *d = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-mm-dd hh-mm-ss"];
    self.name = [[formatter stringFromDate:d] stringByAppendingString:@".png"]; //The name by default of a picture is the date it has been taken
    [formatter release];
    self.path = [pat stringByAppendingPathComponent:self.name];
    if(![self fileExistsAtPath:self.path]){
        [self.data writeToFile:self.path atomically:YES];
    }
    return self;
}

UIImagePickerController非常快,但是当我将图片保存在磁盘上时程序变得非常慢。

对我做错了什么的想法?

2 个答案:

答案 0 :(得分:3)

我有类似的问题。我绕过它的方式是在一个单独的线程中处理来自拾取器的图像。我的问题是当我试图关闭选择器并处理图像时,处理我的app / UI的主要线程崩溃了:

- (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

    NSLog(@"picker did finish");
    [NSThread detachNewThreadSelector:@selector(useImage:) toTarget:self withObject:image];

}

答案 1 :(得分:1)

您的问题可能是由于您拍摄原始图像。

来自相机的原始图像的分辨率大约为1200x1400,这是一个很大的内存,如果您尝试从中制作一张图片,它将导致设备崩溃(它会耗尽内存)。

我建议将图像调整为更小(原生320x480)。