UIImagePickerController run out of memory with camera source

大城市里の小女人 提交于 2019-12-03 08:00:25

问题


I got a big performance issue using UIImagePickerController and saving the image on disk. I can't figure out what I am doing wrong. Here is my code:

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

The addPicture method creates a new picture object that is initialized this way:

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

The UIImagePickerController is quite fast but the program becomes very slow when I save picture on the disk.

Any idea on what I am doing wrong?


回答1:


I had a similar issue. The way I got round it was to handle the image from the picker in a seperate thread. My problem was the main thread handling my app/UI was crashing out when trying to close the picker and handle the image:

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

}



回答2:


Your problem might be due to you taking the original image.

The original image from the camera has a resolution of around 1200x1400, which is a lot of memory and will cause the device to crash if you try making a picture out of it (it will run out of memory).

I would suggest resizing the image to be smaller (the native 320x480).



来源:https://stackoverflow.com/questions/2071947/uiimagepickercontroller-run-out-of-memory-with-camera-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!