我不确定您所说的“安装到 uicollectionviewcell”是什么意思,但以下是您从相册中获取照片的方式。
- 请求授权
- 显示一个 UIImagePickerController(这是苹果为我们创建的默认图像选择器视图)
- 实现委托方法来处理从 UIImagePickerController 中选取的图像
代码如下所示
进口声明:
#import <Photos/Photos.h>
实例化要显示的图像选择器控制器:
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];
}
我相信这通常会提示用户访问照片库,但在简单地尝试显示图像选择器之前处理所有授权情况始终是一种好习惯。
请求授权:
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路由代码将与上述类似。
已添加
照片库只是另一个数据库,但您仍然需要向用户请求授权。
步骤如下:
- 请求授权
- 从照片库中检索照片
我自己还没有完成#2,但似乎有办法做到这一点。
Get all of the pictures from an iPhone photoLibrary in an array using AssetsLibrary framework?
粗略看来,这似乎是一个异步函数,因此您应该进行相应的编码(如果我是您,我会在每个 UICollectionViewCell 中调用 requestImage 函数)。
如果遇到任何问题,请发表评论。
祝你好运!