1.导入所需文件
#import <AssetsLibrary/AssetsLibrary.h>
#import <AssetsLibrary/ALAsset.h>
#import <AssetsLibrary/ALAssetsGroup.h>
#import <AssetsLibrary/ALAssetRepresentation.h>
2.遵守相机的代理
<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>
3.在点击拍摄的事件中
//拍摄按钮点击事件
-(void)palyImage{
NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 判断是否支持相机
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
sourceType = UIImagePickerControllerSourceTypeCamera;
}else{
NSLog(@"不支持相机");
return;
}
// 跳转拍照
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = sourceType;
[self presentViewController:imagePickerController animated:YES completion:^{
}];
}
4.相机代理
//相机的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// 取得图片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// 判断是否是相机拍摄
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
__block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset){
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSString *imageName = [NSString stringWithFormat:@"%@",[imageRep filename]];
NSLog(@"图片名:%@",imageName);
};
ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc]init];
[assetslibrary assetForURL:assetURL resultBlock:resultblock failureBlock:nil];
lib = nil;
}];
}
[picker dismissViewControllerAnimated:YES completion:^{}];
}