我没有工作版本或想法如何通过PHPhotoLibrary或ALAssetsLibrary从您的相册中获取照片,并将其安装在uicollectionviewcell中,就像在Instagram中一样。我是客观c的新手:)
不安装 - 添加
答案 0 :(得分:5)
我不确定你的意思是'安装到uicollectionviewcell'但是这里有你如何从你的专辑中获取照片。
代码如下所示
导入声明:
#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函数)。
如果遇到任何麻烦,请发表评论。 祝你好运!
答案 1 :(得分:0)
您需要声明委托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];
}
}