美文网首页
IOS 拍摄时获取图片名称

IOS 拍摄时获取图片名称

作者: 微笑不是你 | 来源:发表于2017-09-29 09:34 被阅读0次

    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:^{}];

    }

    相关文章

      网友评论

          本文标题:IOS 拍摄时获取图片名称

          本文链接:https://www.haomeiwen.com/subject/yzibextx.html