2014-01-21 52 views
0

我正在創建一個學生索引應用程序,您可以在其中保存學生的姓名,照片和角色。一切正常,但是當我在應用程序測試時創建了一定數量的學生(附圖片)時,該應用程序運行非常緩慢。請找到所附的相應方法。有什麼辦法可以壓縮圖像數據的大小嗎?在保存到核心數據之前壓縮圖像數據

- (IBAction)cameraButtonPressed:(id)sender 
{ 
    if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"no access to camera" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
     [alert show]; 
     return; 
    } 
    UIImagePickerController *controller = [[UIImagePickerController alloc] init]; 
    controller.delegate = self; 
    controller.sourceType = UIImagePickerControllerSourceTypeCamera; 
    [self presentViewController:controller animated:YES completion:nil]; 
} 

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
{ 
    [picker dismissViewControllerAnimated:YES completion:nil];  

    _imageView.image = image; 
    _imageView.contentMode = UIViewContentModeScaleAspectFit; 
} 

- (IBAction)save:(id)sender 
{ 
     Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" 
                  inManagedObjectContext:self.managedObjectContext]; 

    self.student.picdata = UIImagePNGRepresentation(_imageView.image); 

    [self.managedObjectContext save:nil]; 
    [self.delegate DetailStudentSavePressed:self]; 
} 

回答

3

已經壓縮圖像。當你撥打UIImagePNGRepresentation時,你會得到一個PNG風格的壓縮圖像文件。

你不發表您的數據模型的細節,但至少,請確保picdata屬性配置在模型編輯器使用外部存儲。先做。

如果這沒有幫助,還有其他方法可以減少二進制blob對核心數據的影響。但這些不是下一步。您特別提到緩慢而不是內存問題,核心數據和映像的問題更可能導致內存問題。當您遇到速度問題時,請不要擔心圖像處理問題,請使用工具來分析您的應用。你會發現它確切地在哪裏放緩。

+0

非常感謝您的回覆。不幸的是,我無法對你的評論贊不絕口,我需要一個更高的評價才能被允許。 –