2013-07-16 4 views
0

Je veux enregistrer ma photo que j'ai prise dans l'application. Je veux enregistrer l'image de sorte que même si je ferme l'application, la photo est encore enregistrée. voici mon code que je l'ai écrit jusqu'à présentComment enregistrer photo prise

-(IBAction)TakePhoto { 
    picker = [[UIImagePickerController alloc]init]; 
    picker.delegate = self; 
    [picker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    [self presentViewController:picker animated:YES completion:nil]; 
} 

- (IBAction)ChooseExisting { 
    picker2 = [[UIImagePickerController alloc]init]; 
    picker2.delegate = self; 
    [picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
    [self presentViewController:picker2 animated:YES completion:nil]; 
} 

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    [imageview setImage:image]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 

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

Comment dois-je faire l'application enregistrer l'image que j'ai pris et l'afficher.

+0

Vous voulez enregistrer dans le rôle d'appareil photo de l'utilisateur? Ou à l'intérieur de votre propre application? Le premier est juste la fonction C UIImageWriteToSavedPhotosAlbum. – Can

+0

dans ma propre application – Devnews

Répondre

1
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 

UIImage * imageToSave = [UIImage imageNamed:@"Icon.png"]; 
NSData * binaryImageData = UIImagePNGRepresentation(imageToSave); 

[binaryImageData writeToFile:[basePath stringByAppendingPathComponent:@"myfile.png"] atomically:YES]; 

Copied from here

Vous devrez peut-être modifier la ligne avec la variable imageToSave et changer à la variable UIImage, vous avez de la caméra.

1

Essayez ceci dans votre méthode:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    [imageview setImage:image]; 

    //Save Your Image ====== 
    UIImage *yourImage = imageView.image;//imageView.image; 
    NSString *docDirPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
    NSString *filePath = [docDirPath stringByAppendingPathComponent:@"myImageFile.png"]; 
    [UIImagePNGRepresentation(yourImage) writeToFile:filePath atomically:YES]; 



    [self dismissViewControllerAnimated:YES completion:nil]; 

} 

Et dans votre viewDidLoad de la même classe écrire ceci:

//For Get your Image from Documents Dir 
    NSString *docDirPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
    NSString *filePath = [docDirPath stringByAppendingPathComponent:@"myImageFile.png"]; 

    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    { 
//if file already saved then set it on your imageView 
     UIImage *getYourImage = [UIImage imageWithContentsOfFile:filePath]; 
     imageView.image = getYourImage; 
    } 
    else 
    { 
//otherwise set a Dummy Image on your ImageView or nil 
     imageView.image = nil; //[UIImage imageNamed:@"dummyImage.png"]; 

    } 
1

// Appelez cette méthode pour capturer l'image de la caméra

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    //Get image 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    //Display in ImageView object (if you want to display it 
    [imageView setImage:image]; 
     [ButtonName setImage:image forState:UIControlStateNormal]; 
    //Take image picker off the screen (required) 
    [self dismissModalViewControllerAnimated:YES]; 
} 

// pour enregistrer une image:

- (NSString *)saveImage:(UIImage*)image:(NSString*)imageName 
{  
    NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.  
    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it 
    NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path 
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image) 
    NSLog(@"FULL PATH %@",fullPath); 
    NSLog(@"image saved"); 
    return fullPath; 
} 

// pour supprimer une image

- (void)removeImage:(NSString*)fileName 
{ 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]]; 

    [fileManager removeItemAtPath: fullPath error:NULL]; 

    NSLog(@"image removed");  
} 

// pour charger une image de la galerie

- (UIImage*)loadImage:(NSString*)imageName 
{ 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imageName]]; 

    NSLog(@"Loading Image Path : %@",fullPath); 

    return [UIImage imageWithContentsOfFile:fullPath]; 

} 
Questions connexes