swift 相册的自定义
用 swift写的相册自定义,将相册里面的所有图片取出,然后在自定义的页面显示,用这个方法可以写相册图片的多张选择,相册的样式自定义。
代码部分:
import UIKit
import Photos
class ImgPickerVC: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource{
@IBOutlet weak var collectionView: UICollectionView!
//var collectionView:UICollectionView?
///取得的资源结果,用了存放的PHAsset
var assetsFetchResults:PHFetchResult<AnyObject>!
//var fetchResults:PHFetchResult!
///缩略图大小
var assetGridThumbnailSize:CGSize!
/// 带缓存的图片管理对象
var imageManager:PHCachingImageManager!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
//initCollectionView()
//则获取所有资源
let allPhotosOptions = PHFetchOptions()
//按照创建时间倒序排列
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",
ascending: false)]
//只获取图片
allPhotosOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
//assetsFetchResults = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: allPhotosOptions)
let fetchResults:PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: allPhotosOptions)
self.assetsFetchResults = fetchResults as! PHFetchResult <AnyObject>
debugPrint("get all img",fetchResults)
// 初始化和重置缓存
self.imageManager = PHCachingImageManager()
self.resetCachedAssets()
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = UICollectionViewScrollDirection.vertical
let itemWidth = UIScreen.main.bounds.size.width/4 - 6
let itemHeight:CGFloat = 100.0
flowLayout.itemSize = CGSize(width: itemWidth , height: itemHeight)
flowLayout.minimumLineSpacing = 2 //上下间隔
flowLayout.minimumInteritemSpacing = 2 //左右间隔
//self.collectionView?.collectionViewLayout = flowLayout
//self.collectionView = UICollectionView(frame: CGRect(x:0, y:49, width:UIScreen.main.bounds.size.width, height:UIScreen.main.bounds.height-49), collectionViewLayout: flowLayout)
self.collectionView.collectionViewLayout = flowLayout
/**regist collectionCell*/
self.collectionView?.register(ResultCollectionCell.self, forCellWithReuseIdentifier: "resultCollectIdentifer")
self.collectionView?.backgroundColor = UIColor.white
self.view.addSubview(self.collectionView!)
//设置代理
self.collectionView?.delegate = self
self.collectionView?.dataSource = self
//根据单元格的尺寸计算我们需要的缩略图大小
let scale = UIScreen.main.scale
let cellSize = flowLayout.itemSize
assetGridThumbnailSize = CGSize( width:cellSize.width*scale , height:cellSize.height*scale)
}
//重置缓存
func resetCachedAssets(){
self.imageManager.stopCachingImagesForAllAssets()
}
func initCollectionView(){
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = UICollectionViewScrollDirection.vertical
let itemWidth = UIScreen.main.bounds.size.width/4 - 6
let itemHeight:CGFloat = 100.0
flowLayout.itemSize = CGSize(width: itemWidth , height: itemHeight)
assetGridThumbnailSize = flowLayout.itemSize
flowLayout.minimumLineSpacing = 2 //上下间隔
flowLayout.minimumInteritemSpacing = 2 //左右间隔
//self.collectionView = UICollectionView(frame: CGRect(x:0, y:49, width:UIScreen.main.bounds.size.width, height:self.view.bounds.height), collectionViewLayout: flowLayout)
self.collectionView?.collectionViewLayout = flowLayout
self.collectionView?.backgroundColor = UIColor.white
/**regist collectionCell*/
self.collectionView?.register(ResultCollectionCell.self, forCellWithReuseIdentifier: "resultCollectIdentifer")
//注册
//self.collectionView?.register(ZuberImageCell.self,forCellWithReuseIdentifier:"cell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return self.assetsFetchResults!.count
//return 9
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
/**加载cell*/
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "resultCollectIdentifer", for: indexPath) as! ResultCollectionCell
cell.backgroundColor = UIColor.red
let asset = self.assetsFetchResults?[indexPath.row] as! PHAsset
self.imageManager.requestImage(for: asset, targetSize: assetGridThumbnailSize, contentMode: PHImageContentMode.aspectFill, options: nil) { (img, info) in
cell.imgView?.image = img
}
debugPrint("get asset img",asset)
return cell
}
@IBAction func cancleHandle(_ sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
debugPrint("取消图片选择")
}
@IBAction func confirmHandle(_ sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
debugPrint("确认图片选择")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这里有个问题还没有解决,就是不允许访问相册的时候,程序会死掉。。。
swift 相册的自定义相关推荐
- Swift之Codable自定义解析将任意数据类型解析为想要的类型
一.前言 大多数现代应用程序的共同点是,它们需要对各种形式的数据进行编码或解码.无论是通过网络下载的 Json 数据,还是存储在本地的模型的某种形式的序列化表示形式,对于几乎无任何 Swift 代码库 ...
- ios swift 纯代码自定义view(控件) XYswitchView
文章目录 1.截图 2.代码 2.1 XYswitchView.swift 2.2 LoginVC.swift 3.参考博客 1.截图 2.代码 2.1 XYswitchView.swift impo ...
- android 自定义图片裁剪,android 自定义比例裁剪图片(拍照 相册)方式 自定义比例...
先说下我这个有啥好处 主要是能够自定义设置裁剪图片的比例.比如 :今天产品 给你说裁剪 成16:9 的图片. 你做好了 OK 明天 产品又和你说 裁剪成10:7 的图片 ,你是不是要吐血, 我这里只要 ...
- 仿微信修改头像,自定义相册、自定义裁剪
最近因为项目需求需要自定义修改头像时的相册,另外调用系统裁剪经常出现无法裁剪图片的错误,所以裁剪也决定自定义,参照了这两篇文章: https://blog.csdn.net/lvshaorong/ar ...
- swift 从手机选照片_【纯代码】Swift相册照片选择-支持单选或多选
// //NAPublishAssetViewController.swift //Created by on 2019/3/23.//Copyright © . All rights reserve ...
- Swift之使用自定义IconFont
IconFont IconFont 也叫字体图标,顾名思义,就是字体做的图标.受到近些年扁平化设计 的影响,越来越多的图标都开始使用 icon font. IconFont技术起源于Web领域的Web ...
- Swift之SwiftUI自定义star rating评分组件
SwiftUI 让创建自定义 UI 组件变得非常容易,因为它们实际上只是一些带有 @Binding 的视图供我们阅读. 为了演示这一点,我们将构建一个 star rating view,让用户通过点击 ...
- Swift 3DTouch开发 自定义ShortcutItems
3DTouch的分享菜单是上架后系统自动加上的,其他的菜单需要我们自定义. 1.定义菜单项Model import Foundationenum ShortcutItem {case scancase ...
- js调用swift相册DEMO(网易新闻)
关键代码 1 window.location.href = 'tg:///openCamera' css 1 body{ 2 } 3 4 img{ 5 width:100%; 6 } 7 8 #mai ...
最新文章
- GTX1080 安装 CUDA 7.5
- 最简单的t-SNE上手使用
- Android之自定义控件一起制作自定义标签
- ML之回归预测:利用九大类机器学习算法对无人驾驶系统参数(2018年的data,18+2)进行回归预测+评估九种模型性能
- ArrayBlockingQueue队列
- DevExpress v17.2新版亮点—WinForms篇(四)
- jvm内存模型和java内存模型_JVM运行时内存模型综述
- java datetime 转换_如何实现Java日期时间格式转换
- linux主机名的修改
- Netflix推出《DOTA2》系列动画 3月25日上线
- NSDate NSTimerZone 时区转换
- Atitit io读取文件法 目录 1. 文件法	1 1.1. 异步读取文件:	1 1.2. 2.同步读取方法	1 1.3. 二进制读文件:	1 2. 读取api规范	1 3. Atitit 按照
- nginx 配置反向代理
- 1、项目搭建、本地视频列表展示
- CentOS 7 安裝FastDFS V6.0.3
- 亚马逊AWSome DAY
- win10 安装 hadoop 3.3.1报错 Unable to load native-hadoop library
- 720不能建立远程计算机连接,宽带连接错误720:不能建立到远程计算机的连接 正确解决方法...
- cacti graphs new.php,Cacti /graphs_new.php SQL Injection Vulnerability
- 如何做好采购计划和库存管理?
热门文章
- [连载]Java程序设计(01)---任务驱动方式:英制单位转换成公制单位
- 甚么是佛法?(1) (沈家桢博士著)
- Python学习13-15.1-15.12 保持时间、计划任务和启动程序
- 最大化速率的智能反射面波束成形(下): ADMM
- 基于SpringBoot在线答疑管理系统的设计与实现【Java毕业设计·安装调试·代码讲解·文档报告】
- latex解决π派和离散数学投影符号怎么打
- 需要授权的 API ,必须在请求头中使用 Authorization 字段提供 token 令牌
- 2007软件英雄大会,小弟我会众英雄
- html页面如何引用母版页,jquery – 使用带有母版页的Web方法
- Java中秋_【java】中秋节好礼