如何从相机胶卷中选取照片

0 xcode objective-c

我没有工作版本或想法如何通过PHPhotoLibrary或ALAssetsLibrary从您的相册中获取照片,并将其安装在uicollectionviewcell中,如在instagram中.我是客观c的新手:)

不安装 - 添加

jrh*_*e17 5

我不确定'install to uicollectionviewcell'是什么意思,但是这里是你从专辑中获取照片的方式.

  1. 要求授权
  2. 显示UIImagePickerController(这是我们为我们创建的默认图像选择器视图)
  3. 实现委托方法来处理来自UIImagePickerController的拾取图像

代码如下所示

进口声明:

#import <Photos/Photos.h>
Run Code Online (Sandbox Code Playgroud)

实例化图像选择器控制器以显示:

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];
}
Run Code Online (Sandbox Code Playgroud)

我相信这通常会提示用户访问照片库,但在简单地尝试显示图像选择器之前处理所有授权案例总是好的做法.

要求授权:

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
    }
Run Code Online (Sandbox Code Playgroud)

最后实现委托方法:

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

选择图像后,可以将其添加到新实例化的UICollectionViewController中.这可能是另一个问题,你最好还是为此阅读文档.请注意,这是在IOS8中引入的(我认为?),但AL路由代码与上面的类似.


添加

照片库只是另一个数据库,但您仍然需要请求用户授权.步骤如下:

  1. 要求授权
  2. 从照片库中检索照片

我自己没有做过#2,但似乎有办法做到这一点.

使用AssetsLibrary框架从数组中获取iPhone photoLibrary中的所有图片?

从粗略看来,似乎这是一个异步函数,所以你应该相应地编码(如果我是你,我会调用每个UICollectionViewCell中的requestImage函数).

如果遇到任何麻烦,请发表评论.祝好运!