2013-04-03 30 views
1

我由follwing代碼採摘從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。現在它可以不用緩慢地保存圖像。我怎樣才能將圖像保存起來?

回答

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

這將允許主線程在寫出文件時繼續。

+0

thx。但它與 - [CALayer phoneMob]:無法識別的選擇器發送到實例崩潰 – manujmv