AJ分享,必须精品
一:效果
二:代码
相机部分就简单多了,几行代码调用而已,但是如果你要是想实现更多丰富的功能,需要自己写。利用AssetsLibrary.framework,利用这个框架可以获得手机上的所有相册图片,写一个图片选择控制器。
- (void)openCamera
{
[self openImagePickerController:UIImagePickerControllerSourceTypeCamera];
}
- (void)openAlbum
{
// 如果想自己写一个图片选择控制器,得利用AssetsLibrary.framework,利用这个框架可以获得手机上的所有相册图片
// UIImagePickerControllerSourceTypePhotoLibrary > UIImagePickerControllerSourceTypeSavedPhotosAlbum
[self openImagePickerController:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (void)openImagePickerController:(UIImagePickerControllerSourceType)type
{
if (![UIImagePickerController isSourceTypeAvailable:type]) return;
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = type;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
实现代理
#pragma mark - UIImagePickerControllerDelegate
/**
* 从UIImagePickerController选择完图片后就调用(拍照完毕或者选择相册图片完毕)
*/
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
// info中就包含了选择的图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
// 添加图片到photosView中
[self.photosView addPhoto:image];
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// self.picking = NO;
// });
}