UIImagePickerController使用相机源耗尽内存 [英] UIImagePickerController run out of memory with camera source

查看:88
本文介绍了UIImagePickerController使用相机源耗尽内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 UIImagePickerController 我遇到了一个很大的性能问题,并将图像保存在磁盘上。我无法弄清楚我做错了什么。这是我的代码:

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

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

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

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

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?

推荐答案

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

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

}

这篇关于UIImagePickerController使用相机源耗尽内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆