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

还是要区分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];
    
}

你可能感兴趣的:(AssetsLibrary框架,Photos框架,拍摄的照片写入相册)