首先,先了解以下内容:
通过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];
}