Wednesday, 20 November 2013

How to create folder and save multiple image for document directory in ios

.h file

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    UIImagePickerController *picker;
    UIImage *image;
}
-(IBAction)ChooseExisting;

.m file

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

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:NULL];    
    if (image != nil)
    {      
        NSError *error;
        NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *dataPath = [aDocumentsDirectory stringByAppendingPathComponent:@"Image"];
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];       
      
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"yyyyMMddHHmmss"];
        NSDate *now = [NSDate date];
        NSString *retStr = [format stringFromDate:now];        
                
            NSString *anImageName = [NSString stringWithFormat:@"%@.png", retStr];
             NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", dataPath, anImageName];
            NSLog(@"the path is=%@",anImagePath);
            
            NSData *anImageData = UIImagePNGRepresentation(image);
            [anImageData writeToFile:anImagePath atomically:YES];      
       
    }
    
}

No comments:

Post a Comment