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 无限轮播图的两种实现
首先说一下实现的思想: 用UIScrollView实现,在scrollView上添加3个UIImageView,分别用来显示上一张图片,当前显示的图片,下一张图片.scrollView在不滑动的时候永 ...
- js中完数的输出
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- chart.js 示例
一个简单的例子. 1.html代码 <div id="pie" style="width: 250px;float:left"> <h3> ...
- $.unique() 对象组成的数组去掉重复对象
发现一件事,一个完全由对象组成的数组,用$.unique()方法去掉重复的时候不管用 var arr = [{text:'第一个',value:'1'},{text:'第二个',value:'2'}, ...
- ASP.NET MVC3快速入门——第四节、添加一个模型
在本节中我们将追加一些类来管理数据库中的电影.这些类将成为我们的MVC应用程序中的“模型”部分.我们将使用一个.NET Framework的被称之为“Entiry Framework”的数据访问技术来 ...
- 最新版AltiumDesignerSummer9下载+破解
下载地址:ed2k://|file|AltiumDesignerSummer9Build9.3.1.19182.7z|1875307483|e65d364bf987fb5dcfb81c081a1562 ...
- C51 函数/程序段的定位
在Keil C中可能需要指定某个函数或者某段程序链接后存放在程序区中的位置. 1. 如何指定某个函数在程序区中的位置. QUESTION How do I locate a C function at ...
- HMVC
HMVC(Hierarchical-Model-View-Controller),也可以叫做 Layered MVC.顾名思义,就是按等级划分的 MVC 模式,简单的解释就是把MVC又细分成了多个子 ...
- Struts2源代码解读之Action调用
对于Struts2源代码的分析已经有些时日了,虽然网上有很多解读代码,不过自己还是写一个放上来,供大家参考一下. 解读过程: 直接在action类中打断点(包括构造函数和待执行方法)进行debug调试 ...
- bzoj2243-染色(动态树lct)
解析:增加三个变量lc(最左边的颜色),rc(最右边的颜色),sum(连续相同颜色区间段数).然后就是区间合并的搞法.我就不详细解释了,估计你已经想到 如何做了. 代码 #include<cst ...