将拍摄的照片写入相册,并拿到相关参数做相应的处理

还是要区分AssetsLibrary框架与Photos框架。

下面的从手机拍照方法不管是Photos框架还是AssetsLibrary框架下都会调用,只不过将照片写入相册的方法稍有区别。

#pragma mark--从手机拍照
-(void)takePhotoFromiphone{
    
    if ([self judgeIsHaveCameraAuthority]) {
          
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController * picker = [[UIImagePickerController alloc]init];
            picker.delegate = self;
            picker.allowsEditing = NO;
            picker.sourceType = sourceType;
            [self.view.window.rootViewController presentViewController:picker animated:YES completion:nil];
            
        }else{
            NSLog(@"该设备没有摄像头");
        }
        
    }else{
        
        [SVProgressHUD showErrorWithStatus:@"请在iPhone的\"设置-隐私-相机\"中允许访问相机"];
    }
}

AssetsLibrary框架下的代理方法处理

#pragma mark--照片代理
/**
 *  将照片写入相册
 */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    
    UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];

    [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) {
        //取到assetURL做处理
    }];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}
/**
 *  照相取消按钮 退出照相
 */
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

Photos框架下的代理方法处理

#pragma mark--照片代理
/**
 *  将照片写入相册
 */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo{
    
    if(!error){
        PHAsset * asset = [[[PhotoTool sharePhotoTool] getAllAssetInPhotoAblumWithAscending:YES] lastObject];
        //取到asset做相关处理即可
}
/**
 *  照相取消按钮 退出照相
 */
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值