您的位置:首页 > 产品设计 > UI/UE

ios学习--获取通过UIImagePackerController获取的系统相册 图片&视频 的名称信息

2015-12-17 16:06 477 查看
首先,先了解以下内容:

   

通过IUImagePickerController方法获取系统的相册,而想要得到从系统相册得到的图片的信息需要以下几步:

1:获得从UIImagePicker选择的照片的Assert;

2:得到Assert的ALAssertRepresentation;

3:ALAssertRepresentation有个filename的属性

代码如下:该引入的库一定要引入

#import "ViewController.h"

#import <AssetsLibrary/AssetsLibrary.h> 
// 必须导入

#import <AssetsLibrary/ALAsset.h>

#import <AssetsLibrary/ALAssetsGroup.h>

#import <AssetsLibrary/ALAssetRepresentation.h>

@interface
ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super
viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self.view
setBackgroundColor:[UIColor
whiteColor]];

    

    UIButton *uploadDemandButton = [[UIButton
alloc]init];

    [uploadDemandButton setFrame:CGRectMake(50,
50, 100,
50)];

    [uploadDemandButton
setTitle:@"哈哈"
forState:UIControlStateNormal];

    [uploadDemandButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];

    [uploadDemandButton addTarget:self
action:@selector(handleUploadDemand:)
forControlEvents:UIControlEventTouchUpInside];

    [self.view
addSubview:uploadDemandButton];

    

   

    

    

    

    

}

- (void)handleUploadDemand:(id)sender{

    UIImagePickerController *imagePicker = [[UIImagePickerController
alloc]
init];

    imagePicker.delegate =
self;

    imagePicker.modalTransitionStyle =
UIModalTransitionStyleFlipHorizontal;

    imagePicker.allowsEditing =
YES;

    

    

    imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;

    imagePicker.mediaTypes =
@[(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage];

    [self
presentViewController:imagePicker animated:YES
completion:nil];

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    NSLog(@"ishiodfhoisahdfiis");

    NSString *mediaType=[info
objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType
isEqualToString:(NSString *)kUTTypeImage]){

//        //如果是图片

        

    }else{

        //如果是视频

        NSURL *url = info[UIImagePickerControllerReferenceURL];

        

        

        

        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)

        

        {

            

            ALAssetRepresentation *representation = [myasset
defaultRepresentation];

            

            NSString *fileName = [representation
filename];

            

            NSLog(@"fileName : %@",fileName);

            

        };

        

        

        

        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary
alloc] init];

        

        [assetslibrary assetForURL:url

         

                       resultBlock:resultblock 

         

                      failureBlock:nil];

        

        

    }

    

    [picker dismissModalViewControllerAnimated:YES];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: