你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS7技巧 >> ios開發之swift圖片多選,取消選擇模型

ios開發之swift圖片多選,取消選擇模型

編輯:IOS7技巧
swift圖片多選,取消選擇模型的方法就是一段代碼了,下面我們一起來看看在ios開發應用中關於圖片操作。

以下代碼未完全實現多選功能,僅僅為中間過程,作為自己復習使用。因為經過優化的代碼,最終會忘記中間的過程。
讀取本地圖片,然後列出,然後可以多選,可以取消選擇

//
//  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.borderWidth = 1
                        // 最主要的一句
                        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.borderWidth = 1
        // 最主要的一句
        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.borderWidth = 1
        // 最主要的一句
        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
    }

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved