0

I already know how using UIImagePickerController but I'm actually using AVFoundation in my project.

Should I use ImagePickerController's delegate even though I'm using AVFoundation or is there another way to do it?

For the record, the more customizable the better since that is why I've chosen AVFoundation over UIImagePickerController such as, ability to change its layout, its layout's color, its frame, etc.

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
durazno
  • 559
  • 2
  • 7
  • 25
  • How can you use the `UIImagePickerController` delegate if you aren't using `UIImagePickerController`? – rmaddy Aug 08 '15 at 05:14
  • By using AVFoundation to take pictures but accessing Photo Library using UIImagePickerController delegate. – durazno Aug 08 '15 at 05:18
  • you can only use the `UIImagePickerController` delegate if you're using the higher level `UIImagePickerController`. – Michael Dautermann Aug 08 '15 at 05:28
  • Errr did not know that. – durazno Aug 08 '15 at 05:32
  • Guys it works just fine. I don't know why you thought it wouldn't work. Take pictures with AVFoundation, access to Photo Library with UIImagePickerController if you wanted to. – durazno Aug 08 '15 at 07:25
  • Your answer indicates that your question turned out to be very confusing. It seems you are using `AVFoundation` to allow the user to take a photo with a custom camera view and your are using `UIImagePickerController` to allow the user to select a photo from the library. Those two things are completely independent. Your question was asking about using the `UIImagePickerDelegate` with the `AVFoundation` which isn't what anyone would do (and isn't what you are doing in your answer) and why two of us stated you wouldn't do that. (continued) – rmaddy Aug 09 '15 at 05:45
  • Your question should have asked if you could use `AVFoundation` for the camera and `UIImagePickerController` (and its delegate) for selecting from the library. That is a clear "yes you can". – rmaddy Aug 09 '15 at 05:46

3 Answers3

0

Apple has some useful documentation that might help you out.

My guess is that you just need the right keywords to look for.

If you want to support iOS 8 & older iOS versions, what you want to do is get access to some photo in the library as an AVAsset.

The first code listing in that documentation shows how to get the first video from the user's photo album. To get the first photo, simply replace the "[ALAssetsFilter allVideos]" bit with "[ALAssetsFilter allPhotos]".

If you only want to support iOS 9 and newer, there's a new Photos framework, and the assets are accessed as PHAssets.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

Any image available to the Photos app can be accessed using ALAssetsLibrary class. Note that you will have your work cut out for you; you'll have to create the UI for picking images from the Photo Library from scratch.

Reference is available here

Rohit suvagiya
  • 1,005
  • 2
  • 12
  • 40
0

Ok so this is what you do if you absolutely need to customize UI elements of your camera but at the same time you don't feel like getting into AVAsset to bring up Photo Library.

Customize camera with AVFoundation and use UIImagePickerController for Photo Library.

In your custom camera view

- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {

    self.session = [[AVCaptureSession alloc] init];

    [self.session setSessionPreset:AVCaptureSessionPresetPhoto];



    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error;

    self.deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];

    if ([self.session canAddInput:self.deviceInput]) {

        [self.session addInput:self.deviceInput];

    }

    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];

    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    CALayer *rootLayer = [self layer];

    [rootLayer setMasksToBounds:YES];

    CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.width);

    [previewLayer setFrame:frame];

    [rootLayer insertSublayer:previewLayer atIndex:0];

    self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];

    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];

    [self.stillImageOutput setOutputSettings:outputSettings];



    [self.session addOutput:self.stillImageOutput];



    [self.session startRunning];

}
return self;

}

-(void)takePhoto:(UITapGestureRecognizer *)tap{



[UIView animateWithDuration:0.15
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{


                     self.containerView.frame = CGRectMake(-self.frame.size.width, self.containerView.frame.origin.y, self.containerView.frame.size.width, self.containerView.frame.size.height);


                 } completion:^(BOOL finished){
                     if (finished) {



                     }
                 }
 ];


AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in self.stillImageOutput.connections) {
    for (AVCaptureInputPort *port in [connection inputPorts]) {
        if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
            videoConnection = connection;
            break;
        }
    }
}

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
                                                   completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
                                                       if (imageDataSampleBuffer != NULL) {
                                                           NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                           UIImage *image = [UIImage imageWithData:imageData];

                                                           NSLog(@"1. newSize.width : %f, newSize.height : %f",image.size.width,image.size.height);

                                                           [self.session stopRunning];

                                                           [self shouldUploadImage:image];                                                               

                                                       }
                                                   }];

}

-(void)photoAlbumTapped{

NSLog(@"photo album button tapped");

[self.delegate callImagePickerDelegate:self];

}

In VC

-(void)callImagePickerDelegate:(CameraView *)sender{


UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

[self presentViewController:picker animated:YES completion:NULL];

}

durazno
  • 559
  • 2
  • 7
  • 25