Mumthezir VP
Mumthezir VP

Reputation: 6541

Create custom image gallery in iphone

I have used below code to pick photo from gallery or directly using camera

- (void)actionSheet:(UIActionSheet *)actionSheet 
         clickedButtonAtIndex:(NSInteger)buttonIndex {

    UIImagePickerController *imagePickerController = 
      [[UIImagePickerController alloc]init];

    imagePickerController.delegate = self;
    if (buttonIndex == 0) {
        photoButtonNum=0;
        [self presentViewController:imagePickerController 
                           animated:YES 
                         completion:nil];
        imagePickerController.sourceType =  
          UIImagePickerControllerSourceTypePhotoLibrary;
    } else if (buttonIndex == 1) {
        photoButtonNum=1;
        [self presentViewController:imagePickerController 
                           animated:YES 
                         completion:nil];
        imagePickerController.sourceType =  
          UIImagePickerControllerSourceTypeCamera;
    }
}

i am looking to create a custom directory(folder) of my own application to save the picked photos in iPhone. I need your help to

  1. create a custom directory of my own app
  2. want to save the picked photo in that custom directory.

I am a new guy in iPhone development, so waiting for your valuable help. Thanks in advance.

Upvotes: 0

Views: 4047

Answers (2)

iPatel
iPatel

Reputation: 47049

This site is helpful for you to create and save Photo in your Directory

And Also you can use following code.

- (void)imagePickerController:(UIImagePickerController *)picker 
          didFinishPickingMediaWithInfo:(NSDictionary *)info {
     UIImage *pickedImage = 
       [info objectForKey:UIImagePickerControllerOriginalImage];
     NSData *imageData = UIImagePNGRepresentation(pickedImage);

     NSString *documentsDirectory = 
       [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                            NSUserDomainMask, 
                                            YES) lastObject];
     NSString *path = [documentsDirectory 
       stringByAppendingPathComponent:@"imageName.png"];

    NSError * error = nil;
    [imageData writeToFile:storePath options:NSDataWritingAtomic error:&error];

    if (error != nil) {
      NSLog(@"Error: %@", error);
      return;
    }
}

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

iOS 5 give you the feature to add a custom photo album using ALAssetsLibrary

Here is a tutorial iOS5: Saving photos in custom photo album


Edit

In case link becomes inactive

Create ivar in .h file

ALAssetsLibrary* library;

Then in your code probably in viewDidLoad

library = [[ALAssetsLibrary alloc] init];

Then in your delegate method

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [self.library saveImage:image toAlbum:@"Touch Code Magazine" withCompletionBlock:^(NSError *error) {
        if (error!=nil) {
            NSLog(@"Big error: %@", [error description]);
        }
    }];

    [picker dismissModalViewControllerAnimated:NO];
}

Upvotes: 1

Related Questions