选择图像保存会导致速度变慢

时间:2013-04-03 06:27:50

标签: iphone ios objective-c uiimagepickercontroller nscoding

我通过以下代码从iphone中选择了一张图片:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  {
    [self dismissViewControllerAnimated:YES completion:nil];

    UIImage *pickedImg = (UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage];

    [self.photoBtn setBackgroundImage:pickedImg forState:UIControlStateNormal];
    [[self.tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]].contentView addSubview:photoBtn];
    data.img = pickedImg;
    //data.img = nil;
    [self.tblView reloadData];
}

然后通过此代码保存:

-(void)saveProfile  {

    data.firstName = firstName.text;
    data.lastName = lastName.text;
    data.phoneMob = phoneMob.text;
    data.phoneHome = phoneHome.text;
    data.emailOffice = emailOff.text;
    data.emailPersonal = emailPers.text;
    data.address = address.text;
    data.company = company.text;
    data.website = website.text;
    //NSLog(data.img);

    NSMutableData *pData = [[NSMutableData alloc]init];

    NSString *path = [common saveFilePath];

    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:pData];
    [data encodeWithCoder:archiver];
    [archiver finishEncoding];
    [pData writeToFile:path atomically:YES];

    [self.navigationController popViewControllerAnimated:YES];
}

但是当我试图保存配置文件时,它会导致速度变慢。 然后我在第一种方法中尝试了data.img = nil。现在它保存没有慢和没有图像。如何用图像固定保存?

1 个答案:

答案 0 :(得分:0)

您的主要线程被保存操作锁定。将代码写入其自己的线程中的文件:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
    NSMutableData *pData = [[NSMutableData alloc]init];
    NSString *path = [common saveFilePath];

    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:pData];
    [data encodeWithCoder:archiver];
    [archiver finishEncoding];
    [pData writeToFile:path atomically:YES];
});

这将允许主线程在文件写出时继续。

相关问题