Nicky
Nicky

Reputation: 97

Show taken images from camera in a ScrollView

I have a simple application with only one button. When user clicks on it, he can take a pictures, click "Done" in the camera and then can save it and see it on the ScrollView.

However, when he is taking another picture and clicking "Done" in the camera, the new picture overwrites the previous one and he can see only the picture that he has taken.

I want to save all these pictures and then if the user takes 4-5 pictures, he can see all of them by scrolling them.

Here is my code, im not sure why it is not working.

- (IBAction)Camera:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:picker animated:YES completion:NULL];

}


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

    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
    for (UIView *v in [_scrollView subviews]) {
        [v removeFromSuperview];
    }
     [self dismissViewControllerAnimated:YES completion:nil];

    UIImage* image=[info valueForKey:@"UIImagePickerControllerEditedImage"];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
                UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
                [_scrollView addSubview:imageview];
    [images addObject:image];
    self.chosenImages = images;
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    [_scrollView setPagingEnabled:YES];

}

And in header i have an array of chosenImages

@property (nonatomic, copy) NSArray *chosenImages;

Upvotes: 0

Views: 382

Answers (2)

Duncan C
Duncan C

Reputation: 131418

There are lots of things wrong with your code. Every time the user picks an image, you remove all the subviews from your scroll view, then add just the single selected image.

You have an array chosenImages which you might use to hold all the selected images, but you replace it with a new array, images, which only contains the single newly selected image.

You allocate images as a mutable array with enough capacity for the count of the info dictionary you get in didFinishPickingMediaWithInfo, but there's no point to doing that. The count of that dictionary is going to be the number of key/value pairs in the dictionary, which does not reflect the number of images the user selected (I believe the number of images the user selected will always be 1, ignoring the different formats like the original image and the (possible) edited image.)

I would suggest leaving the previous images in your scroll view, tracking the number and placement of those images, and simply adding a new image to the scroll view after the user picks.

You might think about using a UIPageViewController in page scroll mode rather than a raw scroll view. It manages paged scrolling much better than a scrollview will. There is a sample app from Apple called PhotoScroller (link) that even supports tiled rendering of the images, pinch-to-zoom, and panning around an image. It would be pretty easy to stitch that code into your app for handling these images. (You might not decide to deal with the tiled images - I adapted the PhotoScroller code, with tiled image support, into a client project, and generating the image tiles is a fair amount of work.)

Upvotes: 1

avismara
avismara

Reputation: 5149

Assuming that self.chosenImages is of NSMutableArray type, try this

[self.chosenImages addObjectsFromArray:images] 

instead of

self.chosenImages = images;

and if it isn't mutable, you can always set it like so:

NSMutableArray *newArray = [self.chosenImages mutableCopy];
newArray = [newArray addObjectsFromArray:images];
self.chosenImages = [newArray copy];

Upvotes: 0

Related Questions