OStack程序员社区-中国程序员成长平台

标题: ios - UIImagePickerController 用相机源耗尽内存 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:23
标题: ios - UIImagePickerController 用相机源耗尽内存

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

- (void)imagePickerControllerUIImagePickerController *)pick 

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

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

- (Picture*) initPictureNSData*)dat inFolderNSString*)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相当快,但是当我将图片保存在磁盘上时,程序变得很慢。

知道我做错了什么吗?



Best Answer-推荐答案


我遇到了类似的问题。我绕过它的方法是在单独的线程中处理来自选取器的图像。我的问题是处理我的应用程序/UI 的主线程在尝试关闭选择器并处理图像时崩溃了:

- (void)imagePickerControllerUIImagePickerController *)picker
    didFinishPickingImageUIImage *)image
              editingInfoNSDictionary *)editingInfo
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

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

}

关于ios - UIImagePickerController 用相机源耗尽内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2071947/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4