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];
}
通过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];
}
相关文章推荐
- Android Stuio如何将library项目打包成jar文件
- jsp request 对象详解
- UIImageView绕着某一个点旋转
- MYSQL Starting MySQL. ERROR! Manager of pid-file quit without updating file
- Gradle Plugin Samples 之 Gradle Build Variants(六)
- Gradle Plugin Samples 之 Gradle Build Configs(五)
- Java多线程-工具篇-BlockingQueue
- EqualsBuilder类的使用
- iOS中文API之UITouch详解
- [SoapUI] 通过Groovy脚本获取project所在的路径或者直接用${projectDir}
- Webdriver UI自动化测试 实现自动登录
- [EasyUI] Datagrid中文列头显示为乱码
- UITableViewCell自适应内容高度
- 在jsp页面中添加富文本编译器(ueditor)+ 图片上传功能
- Ueditor编辑保存的内容网页显示时背景等信息无效果---参考UEditor官方文档之编辑内容展示
- 沉浸式学习促进教育发展:转变 UI — 用今天的技术设计明天的交互界面
- UIImageJPEGRepresentation和UIImagePNGRepresentation
- IOS-dequeueReusableCellWithIdentifier的应用
- Volley之Post JsonRequest
- easyui 翻译