最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
ios开发之swift图片多选,取消选择模型
时间:2022-06-25 23:42:51 编辑:袖梨 来源:一聚教程网
以下代码未完全实现多选功能,仅仅为中间过程,作为自己复习使用。因为经过优化的代码,最终会忘记中间的过程。
读取本地图片,然后列出,然后可以多选,可以取消选择
//
// ViewController.swift
// imageTest
//
// Created by on 15/11/30.
// Copyright (c) 2015年 JS. All rights reserved.
//
import UIKit
import AssetsLibrary
class ViewController: UIViewController {
//资源库管理类
var assetsLibrary = ALAssetsLibrary()
//保存照片集合
var assets = [ALAsset]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var countOne = 0
var qix = 0;
let registerBtn : UIButton = UIButton()
registerBtn.frame = CGRectMake(10, 200, 300, 40)
registerBtn.backgroundColor = UIColor.lightGrayColor()
registerBtn.setTitle("注册", forState: UIControlState.Normal)
registerBtn.addTarget(self, action: "goRegister:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(registerBtn)
//被读取的数据, 当前函数内无法读取到asset的数据,必须有另外一个动作触发,所以弄了个按钮,下一步动作
//ALAssetsGroupSavedPhotos表示只读取相机胶卷(ALAssetsGroupAll则读取全部相簿)
assetsLibrary.enumerateGroupsWithTypes(ALAssetsGroupSavedPhotos, usingBlock: {
(group: ALAssetsGroup!, stop) in
print("is goin:")
if group != nil
{
let assetBlock : ALAssetsGroupEnumerationResultsBlock = {
(result: ALAsset!, index: Int, stop) in
if result != nil
{
self.assets.append(result)//装载图片
qix = countOne%6;
var pos_x:CGFloat = CGFloat(10 + Int(qix*55) );
var pos_y:CGFloat = CGFloat(40 + Int(countOne/6 * 55));
//取缩略图
let image = UIImage(CGImage:result.thumbnail().takeUnretainedValue())
var imageView = UIImageView(frame: CGRectMake(pos_x, pos_y, 50, 50))
imageView.image = image
imageView.userInteractionEnabled = true;//点击手势
var singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageViewTouch:")
imageView .addGestureRecognizer(singleTap)
imageView.tag = countOne
//边框颜色设置
imageView.layer.borderColor = UIColor.grayColor().CGColor
//边框的宽度设置
imageView.layer.border
// 最主要的一句
imageView.layer.masksToBounds = true
//把uiimageview加载到父控件上,也就是self.view
self.view.addSubview(imageView)
qix++
countOne++
}
}
group.enumerateAssetsUsingBlock(assetBlock)
print("assets:(countOne)")
//collectionView网格重载数据
//self.collectionView?.reloadData()
println("======");
}
}, failureBlock: { (fail) in
print(fail)
})
}
func imageViewTouch(gestur:UITapGestureRecognizer){
println("我被摸了!!");
let x = gestur.view?.tag
print(assets[x!])
let countOne = x //还原countOne
var qix = countOne!%6;
var pos_x:CGFloat = CGFloat(10 + Int(qix*55) );
var pos_y:CGFloat = CGFloat(40 + Int(countOne!/6 * 55));
let image = UIImage(CGImage:assets[x!].thumbnail().takeUnretainedValue())
var imageView = UIImageView(frame: CGRectMake(pos_x, pos_y, 50, 50))
imageView.image = image
imageView.userInteractionEnabled = true;//点击手势
var singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageViewTouch2:")
imageView .addGestureRecognizer(singleTap)
imageView.tag = countOne!
//边框颜色设置
imageView.layer.borderColor = UIColor.redColor().CGColor
//边框的宽度设置
imageView.layer.border
// 最主要的一句
imageView.layer.masksToBounds = true
//把uiimageview加载到父控件上,也就是self.view
self.view.addSubview(imageView)
}
//取消摸
func imageViewTouch2(gestur:UITapGestureRecognizer){
println("我被摸了!!");
let x = gestur.view?.tag
print(assets[x!])
let countOne = x //还原countOne
var qix = countOne!%6;
var pos_x:CGFloat = CGFloat(10 + Int(qix*55) );
var pos_y:CGFloat = CGFloat(40 + Int(countOne!/6 * 55));
let image = UIImage(CGImage:assets[x!].thumbnail().takeUnretainedValue())
var imageView = UIImageView(frame: CGRectMake(pos_x, pos_y, 50, 50))
imageView.image = image
imageView.userInteractionEnabled = true;//点击手势
var singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageViewTouch:")
imageView .addGestureRecognizer(singleTap)
imageView.tag = countOne!
//边框颜色设置
imageView.layer.borderColor = UIColor.grayColor().CGColor
//边框的宽度设置
imageView.layer.border
// 最主要的一句
imageView.layer.masksToBounds = true
//把uiimageview加载到父控件上,也就是self.view
self.view.addSubview(imageView)
}
@IBAction func goRegister(sender: AnyObject) {
//展示图片列表
var count = assets.count;
for var index = 0; index < count; ++index {
println("index is (index)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
更加简洁的办法是我们将imageView存入一个数组,然后利用这个数组来改变图片选择情况
var imgs = [UIImageView]()
//点击,边框变红色
func imageViewTouchArr(gestur:UITapGestureRecognizer){
let x = gestur.view?.tag
var imageView = imgs[x!]
var singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageViewTouchArrCancel:")
imageView .addGestureRecognizer(singleTap)
imageView.layer.borderColor = UIColor.redColor().CGColor
}
//点击取消边框
func imageViewTouchArrCancel(gestur:UITapGestureRecognizer){
let x = gestur.view?.tag
var imageView = imgs[x!]
var singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "imageViewTouchArr:")
imageView .addGestureRecognizer(singleTap)
imageView.layer.borderColor = UIColor.grayColor().CGColor
}
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22