0

我已經知道如何使用UIImagePickerController但我實際上在我的項目中使用AVFoundation使用AVFoundation訪問照片庫

即使我正在使用AVFoundation還是有其他方法可以使用ImagePickerController的代表?

對於備案,更加個性化,因爲這將更好地就是爲什麼我選擇了AVFoundationUIImagePickerController比如,有能力改變其佈局,其佈局的顏色,它的框架等

+0

你怎麼可以,如果你沒有使用'UIImagePickerController'使用'UIImagePickerController'代表? – rmaddy

+0

通過使用AVFoundation拍照,但使用UIImagePickerController委託訪問照片庫。 – durazno

+0

如果您使用更高級別的「UIImagePickerController」,則只能使用「UIImagePickerController」委託。 –

回答

0

蘋果有一些有用的可能會幫助你的文檔。

我的猜測是你只需要正確的關鍵字來尋找。

如果您想要支持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

0

可以使用ALAssetsLibrary類訪問Photos應用程序可用的任何圖像。請注意,您將會爲您剪下您的作品;您必須創建用於從頭開始從照片庫中選取圖像的UI。

參考是available here

0

好了,所以這是你做什麼,如果你確實需要自定義您的相機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]; 

}