【问题标题】:Get access to Photo Library using AVFoundation使用 AVFoundation 访问照片库
【发布时间】:2015-10-31 14:44:08
【问题描述】:

我已经知道如何使用UIImagePickerController,但我实际上在我的项目中使用AVFoundation

我应该使用ImagePickerController 的委托,即使我使用的是AVFoundation,还是有其他方法可以做到这一点?

为了记录,自定义越多越好,因为这就是我选择 AVFoundation 而不是 UIImagePickerController 的原因,例如,能够更改其布局、布局颜色、框架等。

【问题讨论】:

  • 如果不使用UIImagePickerController,如何使用UIImagePickerController 委托?
  • 使用 AVFoundation 拍照但使用 UIImagePickerController 委托访问照片库。
  • 如果您使用更高级别的UIImagePickerController,则只能使用UIImagePickerController 委托。
  • Errr 不知道。
  • 伙计们,它工作得很好。我不知道你为什么认为它不起作用。使用 AVFoundation 拍照,如果需要,可以使用 UIImagePickerController 访问照片库。

标签: ios objective-c avfoundation uiimagepickercontroller photolibrary


【解决方案1】:

好的,如果您绝对需要自定义相机的 UI 元素,但同时又不想进入 AVAsset 来调出照片库,那么这就是您要做的。

使用 AVFoundation 自定义相机,并将 UIImagePickerController 用于照片库。

在您的自定义相机视图中

- (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];

}

在 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];

}

【讨论】:

    【解决方案2】:

    可以使用 ALAssetsLibrary 类访问照片应用程序可用的任何图像。请注意,您的工作会为您完成;您必须从头开始创建用于从照片库中挑选图像的 UI。

    参考是available here

    【讨论】:

      【解决方案3】:

      Apple 有一些有用的文档可能会对您有所帮助。

      我的猜测是你只需要正确的关键字来寻找。

      如果你想支持 iOS 8 及更早的 iOS 版本,你要做的是get access to some photo in the library as an AVAsset

      该文档中的第一个代码清单显示了如何从用户的相册中获取第一个视频。要获得第一张照片,只需将“[ALAssetsFilter allVideos]”位替换为“[ALAssetsFilter allPhotos]”即可。

      如果您只想支持 iOS 9 及更新版本,there's a new Photos framework,资产访问为PHAssets

      【讨论】:

        猜你喜欢
        • 2014-05-30
        • 2020-11-06
        • 2017-10-12
        • 1970-01-01
        • 2020-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-22
        相关资源
        最近更新 更多