Objective c 如何从相机卷中选择照片

Objective c 如何从相机卷中选择照片,objective-c,xcode,Objective C,Xcode,我没有一个工作版本,也不知道如何通过PHPhotoLibrary或AlassetLibrary从相册中获取照片,然后像instagram一样将它们安装在uicollectionviewcell中。我不熟悉目标c:) 不安装-添加我不确定“安装到uicollectionviewcell”是什么意思,但以下是如何从相册中获取照片 请求授权 显示UIImagePickerController(这是apple为我们创建的默认图像选择器视图) 实现委托方法以处理从UIImagePickerControll

我没有一个工作版本,也不知道如何通过PHPhotoLibrary或AlassetLibrary从相册中获取照片,然后像instagram一样将它们安装在uicollectionviewcell中。我不熟悉目标c:)


不安装-添加

我不确定“安装到uicollectionviewcell”是什么意思,但以下是如何从相册中获取照片

  • 请求授权
  • 显示UIImagePickerController(这是apple为我们创建的默认图像选择器视图)
  • 实现委托方法以处理从UIImagePickerController拾取的图像
  • 代码如下所示

    进口声明:

    #import <Photos/Photos.h>
    
    我相信这通常会提示用户访问照片库,但在尝试显示imagepicker之前处理所有授权案例始终是一种好的做法

    请求授权:

    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
        }
    
    最后实现委托方法:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        // Do something with picked image
    }
    
    拾取图像后,可以将其添加到新实例化的UICollectionViewController。这可能是一个完全不同的问题,您最好阅读相关文档。 请注意,这是在IOS8中引入的(我想是吧?),但是AL-route代码与上面的类似


    已添加

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

  • 请求授权
  • 从照片库检索照片
  • 我自己还没有做过,但似乎有办法做到

    粗略地看,这似乎是一个异步函数,因此您应该相应地编写代码(如果我是您,我会在每个UICollectionViewCell中调用requestImage函数)

    如果遇到任何麻烦,请留下评论。
    祝你好运

    我不知道您所说的“安装到uicollectionviewcell”是什么意思,但以下是如何从相册中获取照片

  • 请求授权
  • 显示UIImagePickerController(这是apple为我们创建的默认图像选择器视图)
  • 实现委托方法以处理从UIImagePickerController拾取的图像
  • 代码如下所示

    进口声明:

    #import <Photos/Photos.h>
    
    我相信这通常会提示用户访问照片库,但在尝试显示imagepicker之前处理所有授权案例始终是一种好的做法

    请求授权:

    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
        }
    
    最后实现委托方法:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        // Do something with picked image
    }
    
    拾取图像后,可以将其添加到新实例化的UICollectionViewController。这可能是一个完全不同的问题,您最好阅读相关文档。 请注意,这是在IOS8中引入的(我想是吧?),但是AL-route代码与上面的类似


    已添加

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

  • 请求授权
  • 从照片库检索照片
  • 我自己还没有做过,但似乎有办法做到

    粗略地看,这似乎是一个异步函数,因此您应该相应地编写代码(如果我是您,我会在每个UICollectionViewCell中调用requestImage函数)

    如果遇到任何麻烦,请留下评论。
    祝你好运

    您需要声明委托UIImagePickerControllerDelegate

    像这样

    希望它能帮你很多

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

    您需要声明委托UIImagePickerControllerDelegate

    像这样

    希望它能帮你很多

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

    也许我提的问题不对。当ViewController打开时,我需要从相册中填充UICollectionViewCell照片,而无需借助ImagePicker。如果不是更困难,请解释如何在单元格中一次添加图像,因为我的代码很差。)如果UICollectionViewCell只有一个分区,则应调用[self.imgaray objectAtIndex:[indexPath row]]; 这是一个很好的回答一个模糊的问题。如果我能给你500票,我会的!你显然关心帮助社区作为一个整体,尽管最初的问题措辞拙劣,而且通常不清楚。也许我提出的问题不对。当ViewController打开时,我需要从相册中填充UICollectionViewCell照片,而无需借助ImagePicker。如果不是更困难,请解释如何在单元格中一次添加图像,因为我的代码很差。)如果UICollectionViewCell只有一个分区,则应调用[self.imgaray objectAtIndex:[indexPath row]]; 这是一个很好的回答一个模糊的问题。如果我能给你500票,我会的!你显然关心帮助社区作为一个整体,尽管最初的问题措辞拙劣,而且通常不清楚。也许我提出的问题不对。当ViewController打开时,我需要从相册中填充UICollectionViewCell照片,而不必像这样求助于ImagePicker。也许我提出的问题不对。当ViewController打开时,我需要从相册中填充UICollectionViewCell照片,而不是像这样求助于ImagePicker。这段代码搞糟了,它不符合问题的要求。这段代码搞糟了,它不符合问题的要求。