iOS系统相册的有关操作
iOS中,我们选择相册中的资源和调用摄像头可以使用 :UIImagePickerController类来完成,不使用UI我们可以通过:ALAssetsLibrary类来使用相册资源。
一. ALAssetsLibrary简介
ALAssetsLibrary提供了访问了iOS设备下所有照片和视频的接口
1. 从ALAssetLibrary中可以读取所有的相册数据 用ALAssetsGroup对象列表;
2. 从每个ALAssetsGroup中可获取到其中包含的照片或视频列表,即ALAsset对象列表;
3. 每个ALAsset可能有多个representations表示,即ALAssetRepresentation对象,使用其defaultRepresentation方法可获得其默认representations,使用[asset valueForProperty:ALAssetPropertyRepresentations]可获取其所有representations的数组。
4. 从ALAsset对象可获取缩略图thumbnail或aspectRatioThumbnail;
5. 从ALAssetRepresentation对象可获取全尺寸图片(fullResolutionImage),全屏图片 (fullScreenImage)及图片的各种属性:orientation,dimensions,scale,url,metadata等。
其层次关系为ALAssetsLibrary ->ALAssetsGroup ->ALAsset ->ALAssetRepresentation.
二 . ALAssetsLibrary的使用
ALAssetsLibrary被封装在框架中。所以在使用时,我们需要引入AssetsLibrary.framework 在使用时需要引入头文件:
#import <AssetsLibrary/ALAssetsLibrary.h>
#import <AssetsLibrary/ALAssetsGroup.h>
#import <AssetsLibrary/ALAsset.h>
#import <AssetsLibrary/ALAssetRepresentation.h>
获得相册中的所有图片
ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];
//遍历所有相册
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//遍历每个相册
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
//可以通过valueForProperty获取到图片的信息,包括类型, Location , 时长,方向,日期,格式 , URL地址。
NSString * assetType = [result valueForProperty:ALAssetPropertyType];
if ([assetType isEqualToString:ALAssetTypePhoto]) {
//设置图片的属性
ALAssetRepresentation * assetRepresenttation = [result defaultRepresentation];
CGFloat imageScale = [assetRepresenttation scale];
UIImageOrientation imageOrientation = (UIImageOrientation)[assetRepresenttation orientation];
CGImageRef imageReference = [assetRepresenttation fullResolutionImage];
//对找到的图片进行操作
UIImage * image = [[UIImage alloc] initWithCGImage:imageReference scale:imageScale orientation:imageOrientation];
if (image != nil) {
//获取到所有图片 把所有图片添加到数组中
}else{
NSLog(@"Failed to create the image");
}
}
}];
} failureBlock:^(NSError *error) {
}];
三 . UIImagePickerController详解
iOS获取图片有三种方法
1.直接调用摄像头拍照
2.从相册中选择
3.从图库中选择
UIImagePickerController 是系统提供的来获取图片和视频的接口,用UIImagePickerController类来获取图片视频,大题分为以下几个步骤:
1、初始化UIImagePickerController类
2、设置UIImagePickerController实例的数据来源类型(详情见下);
3、设置代理;
4、如果需要做图片修改的话设置allowsEditing= YES;
数据来源类型一共有三种:
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary, //来自图库
UIImagePickerControllerSourceTypeCamera, //来自手机
UIImagePickerControllerSourceTypeSavedPhotosAlbum //来自相册
} __TVOS_PROHIBITED;
在用这些来源的时候最好检测以下设备是否支持:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
NSLog(@"支持相机");
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
NSLog(@"支持图库");
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
NSLog(@"支持相片库");
}
调用摄像头来获取资源
picker = [[UIImagePickerController alloc]init]; picker.view.backgroundColor = [UIColor orangeColor]; UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera; picker.sourceType = sourcheType; picker.delegate = self; picker.allowsEditing = YES;当用户选取完成后调用:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
选取的信息在info中,info是一个字典
NSString *const UIImagePickerControllerMediaType ;指定用户选择的媒体类型(文章最后进行扩展)NSString *const UIImagePickerControllerOriginalImage ;原始图片NSString *const UIImagePickerControllerEditedImage ;修改后的图片NSString *const UIImagePickerControllerCropRect ;裁剪尺寸NSString *const UIImagePickerControllerMediaURL ;媒体的URLNSString *const UIImagePickerControllerReferenceURL ;原件的URLNSString *const UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效KUTTypeImage 包含:
const CFStringRef kUTTypeImage ;抽象的图片类型const CFStringRef kUTTypeJPEG ;const CFStringRef kUTTypeJPEG2000 ;const CFStringRef kUTTypeTIFF ;const CFStringRef kUTTypePICT ;const CFStringRef kUTTypeGIF ;const CFStringRef kUTTypePNG ;const CFStringRef kUTTypeQuickTimeImage ;const CFStringRef kUTTypeAppleICNS const CFStringRef kUTTypeBMP;const CFStringRef kUTTypeICO;const CFStringRef kUTTypeAudiovisualContent ;抽象的声音视频const CFStringRef kUTTypeMovie ;抽象的媒体格式(声音和视频)const CFStringRef kUTTypeVideo ;只有视频没有声音const CFStringRef kUTTypeAudio ;只有声音没有视频const CFStringRef kUTTypeQuickTimeMovie ;const CFStringRef kUTTypeMPEG ;const CFStringRef kUTTypeMPEG4 ;const CFStringRef kUTTypeMP3 ;const CFStringRef kUTTypeMPEG4Audio ;const CFStringRef kUTTypeAppleProtectedMPEG4Audio;#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
//输入框
UITextView * _textEditor;
//下拉菜单
UIActionSheet * myActionSheet;
//图片2进制路径
NSString * filePath;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationItem.title = @"逆的态度的输入框";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(sendInfo)];
//输入框显示区域
_textEditor = [[UITextView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width , 300)];
_textEditor.backgroundColor = [UIColor brownColor];
//设置它的代理
_textEditor.delegate = self;
_textEditor.keyboardType = UIKeyboardTypeDefault;
_textEditor.font = [UIFont systemFontOfSize:20];
//默认软键盘是不会打开的
[_textEditor becomeFirstResponder];
[self.view addSubview:_textEditor];
//下方的图片按钮 点击后呼出菜单 打开摄像机 查找本地相册
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"car-add" ofType:@"png"]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, _textEditor.frame.size.height + 64, 35, 40);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
//把它也加在视图当中
[self.view addSubview:button];
}
#pragma mark - 打开菜单 -
-(void)openMenu
{
//在这里呼出下方菜单按钮项
myActionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
[myActionSheet showInView:self.view];
[_textEditor resignFirstResponder];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//呼出的菜单按钮点击后的响应
if (buttonIndex == myActionSheet.cancelButtonIndex)
{
NSLog(@"取消");
}
switch (buttonIndex) {
case 0:
//打开照相机拍照
[self takePhoto];
break;
case 1:
//打开本地相册
[self LocalPhoto];
break;
default:
break;
}
}
#pragma mark - 开始拍照 -
- (void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:^{
}];
}else{
NSLog(@"模拟器无法打开照相机,请在真机中使用");
}
}
#pragma mark - 打开本地相册 -
- (void)LocalPhoto
{
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:^{
}];
}
#pragma mark - 发送消息 -
- (void)sendInfo
{
NSLog(@"图片的路径是:%@",filePath);
NSLog(@"你输入框中的内容是:%@",_textEditor.text);
}
#pragma mark - ImagePickerControllerDelegate -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
NSString * type = [info objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:@"public.image"]) {
//先把图片转成NSData
UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData * data;
if (UIImagePNGRepresentation(image) == nil) {
data = UIImageJPEGRepresentation(image, 1.0);
}else{
data = UIImagePNGRepresentation(image);
}
//图片保存的路径
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager * fileManager = [NSFileManager defaultManager];
//把刚刚的图片转换的data对象拷贝至沙盒中 并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,@"/image.png"];
//关闭相册界面
[picker dismissViewControllerAnimated:YES completion:^{
}];
//创建一个选择后图片的小图标放在下方
UIImageView * smallimage = [[UIImageView alloc] initWithFrame:CGRectMake(35, _textEditor.frame.size.height + 68, 35, 35)];
smallimage.image = image;
[self.view addSubview:smallimage];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"你取消了选择图片");
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
@end
iOS系统相册的有关操作的更多相关文章
- ios调用系统相册、相机 显示中文标题、本地化多语言支持
因为调用系统相册.相机需要显示中文,所以搞了半天才知道是在Project->info->Custom ios Target Properties 添加 Localizations 并加入C ...
- iOS开发之保存照片到系统相册(Photo Album)
iOS开发之保存照片到系统相册(Photo Album) 保存照片到系统相册这个功能很多社交类的APP都有的,今天我们简单讲解一下,如何将图片保存到系统相册(Photo Album). 创建UIIma ...
- iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK
iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言: 今天给大家 ...
- [Xcode 实际操作]九、实用进阶-(12)从系统相册中读取图片
目录:[Swift]Xcode实际操作 本文将演示从系统相册中读取图片. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //添加两个协议 ...
- 适配Android4.4~Android11,调用系统相机,系统相册,系统图片裁剪,转换文件(对图片进行上传等操作)
前言 最近Android对于文件的许多方法进行了修改,网络上又没有对Android4到Android11关于系统相机.系统相册和系统裁剪的适配方案,我花了几天事件总结了一下,先上源码 DEMO源码 先 ...
- iOS 获取系统相册数据(不是调系统的相册)
Framework:AssetsLibrary.framework 主要目的是获取到系统相册的数据,并把系统相册里的照片显示出来. 1.创建一个新的项目: 2.将AssetsLibrary.frame ...
- iOS调用系统相册、相机 显示中文标题
解决手机语言已经设置显示中文 在调用系统相册.相机界面 时显示英文问题, 在 info.plist里面添加Localized resources can be mixed YES 表 ...
- iOS开发之获取系统相册ALAssetLibrary
注:当你选择看这篇博客时想必你的应用还支持iOS8一下系统,如果你的应用要求最低版本大于iOS8,建议使用PhotoKit框架,效率更高 ALAssetsLibrary包含,ALAssetsLibra ...
- iOS 调用系统相册 相机 时,显示中文标题
解决手机语言已经设置显示中文 在调用系统相册.相机界面 时显示英文问题, 在 info.plist里面添加Localized resources can be mixed YES 表示是否允许应用程序 ...
随机推荐
- (转载)iOS 多媒体
音频:(音效.音乐) 在iOS中音频播放从形式上可以分为音效播放和音乐播放.前者主要指的是一些短音频播放,通常作为点缀音频,对于这类音频不需要进行进度.循环等控制.后者指的是一些较长的音频,通常是主音 ...
- hdu5353 Average(模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Average Time Limit: 4000/2000 MS (Java/Ot ...
- Java系列--第二篇 基于Maven的Android开发HelloAndroidWorld
曾经写过一篇Android环境配置的随笔,个人感觉特繁琐,既然有Maven,何不尝试用用Maven呢,经网上搜索这篇文章但不限于这些,而做了一个基于Maven的Android版的Hello Andro ...
- HTML5画布(线条、渐变)
绘制直线时,一般会用到moveTo与lineTo两种方法. 案例1: <!DOCTYPE html><html><head lang="en"> ...
- destoon控制标题长度,title中显示全标题 标题字符长度怎么控制?
如题商品调用出来后,标题的字符长度怎么控制?有哪位高手能帮我解决吗? 小弟在此感谢了. &length=30 //30表示30个字节 <!--{tag("moduleid=5& ...
- CSS3学习之圆角box-shadow,阴影border-radius
最近经常玩腾讯微博,出来职业习惯,看看它的CSS,里面运用了大量的css3的东东,有一处用到了Data URI,还有css expression有争议的地方,对png24图片的处理也是用滤镜,类似( ...
- 使用div+iframe实现弹窗及弹出内容无法显示的解决
使用div+iframe实现弹窗 除了使用实际的弹出窗口,还可以使用控制一个div的display属性来模拟一个弹出窗口的操作,这里使用在Div里放一个iFrame的方式,主要考虑到可以在需要的时候加 ...
- 使用grid++report打印选中行
接上一篇<hibernate+spring+mvc+Easyui框架模式下使用grid++report的总结>对grid++report做进一步开发 先写一下实现流程: 1.默认为全部载入 ...
- EF查询
public ActionResult AllSettings(DataSourceRequest command, Framework.Kendoui.Filter filter = null, S ...
- qt之fillder抓包(QT网络版有一些具体的坑)
最近项目中使用到了Qt的网络库,在用的过程中也发现了不少坑和问题,本文仅仅作为记录,方便日后查阅. 因为我们整个客户端的gui都是使用qt来完成的,心想qt既然有网络库,而且真心觉着qt封装的控 ...