2012-10-31 2 views
0

J'essaie de faire une application simple pour prendre une photo et enregistrer le fichier à la fois dans un UIImageview et dans le dossier de documents de sorte qu'il peut être rappelé à plus tard comme le UIImageview. Pour avoir une visibilité sur la création d'un fichier par l'application, j'ai activé l'application qui prend en charge le partage de fichiers iTunes dans le fichier .plist.enregistrer une image dans le répertoire documents - ne fonctionne pas - créer uniquement un fichier 0 octet

Problème: le fichier généré est zéro octet. J'ai essayé le format PNG et JPG.

- (IBAction)picImage:(id)sender { 
imagePicker = [[UIImagePickerController alloc] init]; 
imagePicker.delegate = self; 

//check to see if the device has camera capability 
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
{ 
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 

} 
else 
{ 
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
} 

[self presentViewController:imagePicker animated:YES completion:nil]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    //set the UIImageView to the selected image 
    playerImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    //obtaining saving path 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"player.png"]; 

    UIImage *editedImage = [UIImage imageWithContentsOfFile:imagePath]; 
    NSData *imageData = UIImagePNGRepresentation(editedImage); 
    //NSData *imageData = UIImageJPEGRepresentation(editedImage,1.0); 

    // This actually creates the image at the file path specified, with the image's NSData. 
    [[NSFileManager defaultManager] createFileAtPath:imagePath contents:imageData attributes:nil]; 

    //dismiss the imagepicker view controller 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
[self dismissViewControllerAnimated:YES completion:nil]; 
} 

Répondre

1

Essayez ce code et laissez-moi savoir si cela fonctionne ou non .. !!!!

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *strimagename; 
    strimagename=[NSString stringWithFormat:@"Test.jpg"]; 


NSString *thumbFilePath = [documentsDirectory stringByAppendingPathComponent:strimagename]; 
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
NSData *thumbData = UIImageJPEGRepresentation(image, 1); 
[thumbData writeToFile:thumbFilePath atomically:YES]; 
[imgPicker dismissModalViewControllerAnimated:YES]; 

Ce code permettra d'économiser l'image dans le dossier documentory ..

Bonne programmation .. !!!!!!

+0

Merci! Cela a fonctionné avec deux petites modifications à la dernière ligne. J'ai dû changer imgPicker en moi-même et changer la fonction abandonnée de rejectModal ... en celle mise à jour. - [self dismissViewControllerAnimated: YES achèvement: néant]; – jdeason

+0

Bon travail ..... !!!! – NiravPatel

Questions connexes