0

I do not have a working version or idea how to get photos from your albums via PHPhotoLibrary or ALAssetsLibrary and install them in uicollectionviewcell like in instagram. I'm new to objective c :)

not install - add

m9st
  • 5
  • 1
  • 6
  • maybe I'm not right posed the question . I need to fill UICollectionViewCell photos from an album when ViewController opening without resorting to ImagePicker like this – m9st Mar 25 '16 at 08:57

2 Answers2

6

I'm not sure what you mean by 'install to uicollectionviewcell' but here's how you would get photos from your album.

  1. Ask for authorization
  2. Show a UIImagePickerController (which is the default image picker view apple created for us)
  3. Implement delegate methods to handle the picked image from the UIImagePickerController

Code would look as follows

Import statement:

#import <Photos/Photos.h>

Instantiate the image picker controller to show:

UIImagePickerController* imagePicker = [[UIImagePickerController alloc]init];
// Check if image access is authorized
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // Use delegate methods to get result of photo library -- Look up UIImagePicker delegate methods
    imagePicker.delegate = self;
    [self presentViewController:imagePicker animated:true completion:nil];
}

I believe this would usually prompt the user for access to the photo library, but it's always good practice to handle all cases of authorization prior to simply trying to show the imagepicker.

Ask for authorization:

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if(status == PHAuthorizationStatusNotDetermined) {
        // Request photo authorization
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            // User code (show imagepicker)
    }];
    } else if (status == PHAuthorizationStatusAuthorized) {
        // User code
    } else if (status == PHAuthorizationStatusRestricted) {
        // User code
    } else if (status == PHAuthorizationStatusDenied) {
        // User code
    }

Finally implement delegate methods:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    // Do something with picked image
}

After you pick your image, you can add it to a newly instantiated UICollectionViewController. This is probably a whole other question and you would be better off reading documentation for this. Note that this was introduced in IOS8(I think?), but the AL route code will be similar to the above.


Added

The photo library is just another database, but you still have to ask the user for authorization. The steps would be as follows:

  1. Ask for authorization
  2. Retrieve photos from photo library

I haven't done #2 myself but there seems to be a way to do it.

Get all of the pictures from an iPhone photoLibrary in an array using AssetsLibrary framework?

From a cursory look, it seems like this is an asynchronous function so you should code accordingly (I would call the requestImage function inside each UICollectionViewCell if I were you).

Leave a comment if you run into any trouble. Good Luck!

Community
  • 1
  • 1
jrhee17
  • 1,152
  • 9
  • 19
  • maybe I'm not right posed the question . I need to fill UICollectionViewCell photos from an album when ViewController opening without resorting to ImagePicker [like this](https://pp.vk.me/c630917/v630917473/23494/3GpCH5S502M.jpg) – m9st Mar 25 '16 at 08:03
  • if not more difficult , explain how to add an image at a time in the cells , because my code is poor ) ) [1st part](https://pp.vk.me/c631129/v631129473/24a93/sEra0zWN23s.jpg) [2nd part](https://pp.vk.me/c631129/v631129473/24a9d/G6l3ULctLEo.jpg) – m9st Mar 28 '16 at 12:13
  • If you have a single section for your UICollectionViewCell, then you should call [self.imgArray objectAtIndex:[indexPath row]]; Refer to https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/ – jrhee17 Mar 28 '16 at 13:23
  • This is a great answer for a vague question. If I could give you 500 upvotes, I would! You clearly care to help the community as a whole here, even though the original question was poorly worded and generally unclear. – jungledev Jul 27 '17 at 00:45
0

You need to declare delegate UIImagePickerControllerDelegate

like this..

hope its helps you a lot...

- (IBAction)captureImagehandler:(id)sender
{   
    if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:@"No Device" message:@"Camera is not available"
                                                             delegate:nil
                                                    cancelButtonTitle:@"exit"
                                                        otherButtonTitles:nil];
    [deviceNotFoundAlert show];

} 
else 
{

    UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
    cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    cameraPicker.delegate =self;
    // Show image picker
    [self presentViewController:cameraPicker animated:YES completion:nil];
}
}
Akash Raghani
  • 557
  • 1
  • 9
  • 21