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;
上面只是实例化了UIImagePickerController及其属性 在需要获取图片的时候需要弹出窗口调用
 
  [self presentViewController:picker animated:YES completion:nil];

当用户选取完成后调用:

- (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 ;媒体的URL
NSString *const  UIImagePickerControllerReferenceURL ;原件的URL
NSString *const  UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效
 
UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie

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;
 
KUTTypeMovie 包含:
 
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系统相册的有关操作的更多相关文章

  1. ios调用系统相册、相机 显示中文标题、本地化多语言支持

    因为调用系统相册.相机需要显示中文,所以搞了半天才知道是在Project->info->Custom ios Target Properties 添加 Localizations 并加入C ...

  2. iOS开发之保存照片到系统相册(Photo Album)

    iOS开发之保存照片到系统相册(Photo Album) 保存照片到系统相册这个功能很多社交类的APP都有的,今天我们简单讲解一下,如何将图片保存到系统相册(Photo Album). 创建UIIma ...

  3. iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK

    iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言:    今天给大家 ...

  4. [Xcode 实际操作]九、实用进阶-(12)从系统相册中读取图片

    目录:[Swift]Xcode实际操作 本文将演示从系统相册中读取图片. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //添加两个协议 ...

  5. 适配Android4.4~Android11,调用系统相机,系统相册,系统图片裁剪,转换文件(对图片进行上传等操作)

    前言 最近Android对于文件的许多方法进行了修改,网络上又没有对Android4到Android11关于系统相机.系统相册和系统裁剪的适配方案,我花了几天事件总结了一下,先上源码 DEMO源码 先 ...

  6. iOS 获取系统相册数据(不是调系统的相册)

    Framework:AssetsLibrary.framework 主要目的是获取到系统相册的数据,并把系统相册里的照片显示出来. 1.创建一个新的项目: 2.将AssetsLibrary.frame ...

  7. iOS调用系统相册、相机 显示中文标题

    解决手机语言已经设置显示中文 在调用系统相册.相机界面 时显示英文问题, 在 info.plist里面添加Localized resources can be mixed          YES 表 ...

  8. iOS开发之获取系统相册ALAssetLibrary

    注:当你选择看这篇博客时想必你的应用还支持iOS8一下系统,如果你的应用要求最低版本大于iOS8,建议使用PhotoKit框架,效率更高 ALAssetsLibrary包含,ALAssetsLibra ...

  9. iOS 调用系统相册 相机 时,显示中文标题

    解决手机语言已经设置显示中文 在调用系统相册.相机界面 时显示英文问题, 在 info.plist里面添加Localized resources can be mixed YES 表示是否允许应用程序 ...

随机推荐

  1. iOS在MRC工程环境下下使用ARC的方法

  2. Sql 函数大全 (更新中...由难到简

    1.字符处理类: 1.1 指定指定字符输出的次数 ) 结果:1a1a1a1a1a (5个1a)

  3. css实现超连接按钮形式显示

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. call()与apply()传参需要注意的一点

    call()与apply()是用来改变函数体内的this指向,第一个参数是调用函数的母对象,他是调用上下文,函数体内通过this来获得对它的引用,换句话说就是第一参数===函数中的this. 但是如下 ...

  5. Go学习笔记(一):Ubuntu 环境下Go的安装

    本文是根据<Go Web 编程>,逐步学习 Ubuntu 环境下go的安装的笔记. <Go Web 编程>的URL地址如下: https://github.com/astaxi ...

  6. HDU 4507 有点复杂却不难的数位DP

    首先来说,,这题我wrong了好几次,代码力太弱啊..很多细节没考虑.. 题意:给定两个数 L R,1 <= L <= R <= 10^18 :求L 到 R 间 与 7 无关的数的平 ...

  7. 一句SQL实现MYSQL的递归查询

    众所周知,目前的mysql版本中并不支持直接的递归查询,但是通过递归到迭代转化的思路,还是可以在一句SQL内实现树的递归查询的.这个得益于Mysql允许在SQL语句内使用@变量.以下是示例代码. 创建 ...

  8. C++ crash 堆栈信息获取(三篇文章)

    最近在做程序异常时堆栈信息获取相关工作,上一篇文章成功的在程序creash时写下了dump文件,而有些情况写dump文件是 不可以的,比如在jni开发时,C++只做底层处理,而整个项目是android ...

  9. .NET 使用unity实现依赖注入

    原文地址:http://www.cnblogs.com/wujy/p/3317795.html 一:理论部分 依赖注入:这是 Ioc 模式的一种特殊情况,是一种基于改变对象的行为而不改变类的内部的接口 ...

  10. OpenWRT 编译 error GNU libiconv not in use but included iconv.h is from...

    OpenWRT 编译 error GNU libiconv not in use but included iconv.h is from... 编译的时候碰到一个常见的错误,但是却在一个陌生的地方爆 ...