Swift image selection for iOS development, deselect model

Source: Internet
Author: User

The following code does not fully implement the multiple-selection function, just for the intermediate process, as a review of their use. Because of the optimized code, you'll end up forgetting the middle process.
Read the local picture, then list, then select multiple, you can deselect

//
Viewcontroller.swift
Imagetest
//
Created by on 15/11/30.
Copyright (c) 2015 JS. All rights reserved.
//

Import Uikit
Import Assetslibrary

Class Viewcontroller:uiviewcontroller {
Resource Pool Management class
var assetslibrary = Alassetslibrary ()
Save Photo Collection
var assets = [Alasset] ()

Override Func Viewdidload () {
Super.viewdidload ()
Do no 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 ("registered", ForState:UIControlState.Normal)
Registerbtn.addtarget (Self, Action: "Goregister:", ForControlEvents:UIControlEvents.TouchUpInside)
Self.view.addSubview (REGISTERBTN)


Read data, the current function can not read the asset data, there must be another action trigger, so get a button, the next action
Alassetsgroupsavedphotos means read only camera film (Alassetsgroupall reads all albums)
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)//Load Picture
Qix = countone%6;

var pos_x:cgfloat = cgfloat (ten + Int (qix*55));
var pos_y:cgfloat = cgfloat (+ Int (COUNTONE/6 * 55));

Take thumbnails
Let image = UIImage (CGImage:result.thumbnail (). Takeunretainedvalue ())
var ImageView = Uiimageview (Frame:cgrectmake (pos_x, Pos_y, 50, 50))
Imageview.image = Image
imageview.userinteractionenabled = true;//Click gesture
var Singletap:uitapgesturerecognizer = UITapGestureRecognizer (target:self, Action: "Imageviewtouch:")
ImageView. Addgesturerecognizer (SINGLETAP)
Imageview.tag = Countone

Border color settings
ImageView.layer.borderColor = Uicolor.graycolor (). Cgcolor
Width setting for borders
ImageView.layer.borderWidth = 1
One of the most important sentences
ImageView.layer.masksToBounds = True
Load the Uiimageview onto the parent control, which is the Self.view
Self.view.addSubview (ImageView)


qix++
countone++

}
}


Group.enumerateassetsusingblock (Assetblock)
Print ("Assets:\ (Countone)")
CollectionView Grid overloaded Data
Self.collectionview? Reloaddata ()
println ("= = =");
}

}, Failureblock: {(fail) in
Print (fail)
})

}

Func Imageviewtouch (Gestur:uitapgesturerecognizer) {
println ("I was touched!!");
Let x = Gestur.view? Tag

Print (assets[x!])

Let Countone = x//Restore Countone
var qix = countone!%6;

var pos_x:cgfloat = cgfloat (ten + Int (qix*55));
var pos_y:cgfloat = cgfloat (+ 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;//Click gesture
var Singletap:uitapgesturerecognizer = UITapGestureRecognizer (target:self, Action: "IMAGEVIEWTOUCH2:")
ImageView. Addgesturerecognizer (SINGLETAP)
Imageview.tag = countone!

Border color settings
ImageView.layer.borderColor = Uicolor.redcolor (). Cgcolor
Width setting for borders
ImageView.layer.borderWidth = 1
One of the most important sentences
ImageView.layer.masksToBounds = True
Load the Uiimageview onto the parent control, which is the Self.view
Self.view.addSubview (ImageView)

}

Cancel Touch
Func ImageViewTouch2 (Gestur:uitapgesturerecognizer) {
println ("I was touched!!");
Let x = Gestur.view? Tag

Print (assets[x!])

Let Countone = x//Restore Countone
var qix = countone!%6;

var pos_x:cgfloat = cgfloat (ten + Int (qix*55));
var pos_y:cgfloat = cgfloat (+ 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;//Click gesture
var Singletap:uitapgesturerecognizer = UITapGestureRecognizer (target:self, Action: "Imageviewtouch:")
ImageView. Addgesturerecognizer (SINGLETAP)
Imageview.tag = countone!

Border color settings
ImageView.layer.borderColor = Uicolor.graycolor (). Cgcolor
Width setting for borders
ImageView.layer.borderWidth = 1
One of the most important sentences
ImageView.layer.masksToBounds = True
Load the Uiimageview onto the parent control, which is the Self.view
Self.view.addSubview (ImageView)

}



@IBAction func Goregister (sender:anyobject) {

Show a list of pictures
var count = Assets.count;

for var index = 0; Index < count; ++index {
println ("index is \ (index)")
}
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
Dispose of any of the can is recreated.
}


}
A more concise approach is to save the ImageView in an array and then use the array to change the picture selection.
var IMGs = [Uiimageview] ()

Click, the border turns red
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
}
Click to cancel the border
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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.