2012-08-07 83 views
-1

我想要保存一個已被帶到應用程序文檔目錄的圖像,但由於某種原因,計數器總是在增加,但目錄中沒有圖像。什麼似乎是問題?圖像不會保存到文檔目錄

非常感謝!

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info 
{ 
    [picker dismissModalViewControllerAnimated:YES]; 
    self.imageViewRecipt.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

    // 
    // Getting the current counter value 
    // 
    NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults]; 

    int imageCounter; 
    imageCounter = [[prefs objectForKey:@"imageCounter"]intValue]; 

    // 
    // Obtaining saving path 
    // 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"image%i.png",imageCounter]]; 

    // 
    // Extracting image from the picker and saving it 
    // 
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    if ([mediaType isEqualToString:@"public.image"]) 
    { 
     UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
     NSData *webData = UIImagePNGRepresentation(editedImage); 
     [webData writeToFile:imagePath atomically:YES]; 

     // 
     // Saving the new counter into the plist 
     // 
     imageCounter++; 

     [prefs setInteger:imageCounter forKey:@"imageCounter"]; 
     [prefs synchronize]; 
    } 
} 
+0

非常感謝朋友。 – 2012-08-07 07:31:30

回答

0

OK,所以我固定它通過刪除「mediaType的」,而如果statment以及與此替換:

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info 
{ 
    [picker dismissModalViewControllerAnimated:YES]; 
    self.imageViewRecipt.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

    // 
    // Getting the current counter value 
    // 
    NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults]; 

    int imageCounter; 
    imageCounter = [[prefs objectForKey:@"imageCounter"]intValue]; 

    // 
    // Obtaining saving path 
    // 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"image%i.png",imageCounter]]; 

    // 
    // Extracting image from the picker and saving it 
    // 
    UIImage *editedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    NSData *webData = UIImagePNGRepresentation(editedImage); 
    [webData writeToFile:imagePath atomically:YES]; 
    NSData *webData = UIImagePNGRepresentation(editedImage); 
    [webData writeToFile:imagePath atomically:YES]; 

    // 
    // Saving the new counter into the plist 
    // 
    imageCounter++; 

    [prefs setInteger:imageCounter forKey:@"imageCounter"]; 
    [prefs synchronize]; 
} 

,現在它的工作就好了。

相關問題