DavidJ
DavidJ

Reputation: 27

How to display multiple imported photos with objective c

I am trying to make an app that has the import page similar to VSCO app. But i can`t figure how to display all the pictures in a view after the user selects multiple photos from the gallery, the maximum should be 10 photos.

To get the image i used this code and it works good for one picture selection:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:false completion:nil];

// transfer to the main screen
dispatch_async(dispatch_get_main_queue(), ^{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PreviewVC *prevVC = (PreviewVC *)[storyboard instantiateViewControllerWithIdentifier:@"PreviewVC"];


    passedImage = image;

    prevVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:prevVC animated:true completion:nil];
});

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:false completion:nil];

// Go to the Main Screen
dispatch_async(dispatch_get_main_queue(), ^{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PreviewVC *prevVC = (PreviewVC *)[storyboard instantiateViewControllerWithIdentifier:@"PreviewVC"];

    // Passing Image to Preview VC
    passedImage = image;

    prevVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:prevVC animated:true completion:nil];
});

}

After i used a preview view to show the imported photo before you can edit it:

@implementation PreviewVC

// Hide the Status Bar
- (BOOL)prefersStatusBarHidden {
    return true;
}

-(UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a bitmap context.
    UIGraphicsBeginImageContextWithOptions(newSize, true, image.scale);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

Upvotes: 0

Views: 444

Answers (1)

danh
danh

Reputation: 62686

If the idea is to build up several images in the PreviewVC, then have it launch the picker and have it keep an array of images as a property. That image array in turn becomes the datasource for a table view in the preview vc. In rough outline...

// in PreviewVC.m
// in the private interface
@property (nonatomic, weak) IBOutlet UITableView *tableView;  // attach this in IB, don't forget to set datasource to this vc
@property (nonatomic, strong) NSArray *images;

- (void)userDidPressPickImageButton:(id)sender {
    // launch image picker vc from here
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:false completion:nil];
    image = [self imageWithImage:image scaledToSize:someSize]; // move this method here, too

    // here's the key
    [self.images addObject:image];
    [self.tableView reloadData];
}

Make your table view datasource answer the self.images.count for numberOfRows, and, of course, cellForRow must configure a cell using self.images[indexPath.row]

Upvotes: 1

Related Questions