1

我通过以下代码从 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了第一种方法。现在它没有缓慢和没有图像保存。如何用图像固定保存?

4

1 回答 1

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

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

于 2013-04-03T06:31:52.630 回答