PhotoKit 4 Delete an album

Swift 3.0 Info.plistにNSPhotoLibraryUsageDescriptionを追加します // // ViewController.swift // phptokit004 // // Copyright © 2016年 FaBo, Inc. All rights reserved. // import UIKit import Photos extension PHPhotoLibrary { // ユーザーに許可を促す. class func Authorization(){ PHPhotoLibrary.requestAuthorization { (status) -> Void in switch status { case .authorized: print("Authorized") case .denied: print("Denied") case .notDetermined: print("NotDetermined") case .restricted: print("Restricted") } } } } class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var deleteTarget: PHAssetCollection! // アルバム. var myAlbum: [String] = [] override func viewDidLoad() { // フォトアプリの中にあるアルバムを検索する. let list = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.album, subtype: PHAssetCollectionSubtype.any, options: nil) // リストの中にあるオブジェクトに対して1つずつ呼び出す. for i in 0 ..< list.count { let item = list.object(at: i) // アルバムのタイトル名をコレクションする. myAlbum.append(item.localizedTitle!) } // 結果表示用のTableViewを用意. let barHeight = UIApplication.shared.statusBarFrame.size.height let displayWidth = self.view.frame.width let displayHeight = self.view.frame.height // TableViewを生成. let myTableView: UITableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight)) myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") myTableView.dataSource = self myTableView.delegate = self self.view.addSubview(myTableView) } // MARK: - UITableViewDelegate // Cellがタッチされた時に呼ばれるメソッド. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // フォトアプリの中にあるアルバムを検索する. let list = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.album, subtype: PHAssetCollectionSubtype.any, options: nil) // リストの中にあるオブジェクトに対して1つずつ呼び出す. for i in 0 ..< list.count { // 選択した名前と同じ名前のアルバムを取得. let item = list.object(at: i) if item.localizedTitle == myAlbum[indexPath.row] { // 削除する対象のアルバムを代入する. self.deleteTarget = item } } // フォトライブラリに変更を要請する. PHPhotoLibrary.shared().performChanges({ // 削除対象のアルバムを引数に渡す. PHAssetCollectionChangeRequest.deleteAssetCollections([self.deleteTarget]) }, completionHandler: nil) } // MARK: - UITableViewDataSource // Cellの数を返すメソッド. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myAlbum.count } // Cellの初期化をするメソッド. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) // Cellに値を設定. cell.textLabel?.text = "\(myAlbum[indexPath.row])" return cell } } Swift 2.3 // // ViewController.swift // phptokit004 // // Copyright © 2016年 FaBo, Inc. All rights reserved. // import UIKit import Photos extension PHPhotoLibrary { // ユーザーに許可を促す. class func Authorization(){ PHPhotoLibrary.requestAuthorization { (status) -> Void in switch status { case .Authorized: print("Authorized") case .Denied: print("Denied") case .NotDetermined: print("NotDetermined") case .Restricted: print("Restricted") } } } } class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var deleteTarget: PHAssetCollection! // アルバム. var myAlbum: [String] = [] override func viewDidLoad() { // フォトアプリの中にあるアルバムを検索する. let list = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.Album, subtype: PHAssetCollectionSubtype.Any, options: nil) // リストの中にあるオブジェクトに対して1つずつ呼び出す. list.enumerateObjectsUsingBlock { (album, index, isStop) -> Void in // アルバムのタイトル名を配列にコレクションする. self.myAlbum.append(album.localizedTitle) } // 結果表示用のTableViewを用意. let barHeight = UIApplication.sharedApplication().statusBarFrame.size.height let displayWidth = self.view.frame.width let displayHeight = self.view.frame.height // TableViewを生成. let myTableView: UITableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight)) myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") myTableView.dataSource = self myTableView.delegate = self self.view.addSubview(myTableView) } // MARK: - UITableViewDelegate // Cellがタッチされた時に呼ばれるメソッド. func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { // フォトアプリの中にあるアルバムを検索する. let list = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.Album, subtype: PHAssetCollectionSubtype.Any, options: nil) // リストの中にあるオブジェクトに対して1つずつ呼び出す. list.enumerateObjectsUsingBlock { (album, index, isStop) in // 選択した名前と同じ名前のアルバムを取得. if album.localizedTitle == self.myAlbum[indexPath.row] { // 削除する対象のアルバムを代入する. self.deleteTarget = album as! PHAssetCollection } } // フォトライブラリに変更を要請する. PHPhotoLibrary.sharedPhotoLibrary().performChanges({ // 削除対象のアルバムを引数に渡す. PHAssetCollectionChangeRequest.deleteAssetCollections([self.deleteTarget]) }, completionHandler: nil) } // MARK: - UITableViewDataSource // Cellの数を返すメソッド. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myAlbum.count } // Cellの初期化をするメソッド. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) // Cellに値を設定. cell.textLabel?.text = "\(myAlbum[indexPath.row])" return cell } } 2.xと3.xの差分 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Intからfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Intに変更 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {からfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCellに変更 UITableView.dequeueReusableCellWithIdentifier(String, forIndexPath: NSIndexPath)からUITableView.dequeueReusableCell(withIdentifier: String, for: IndexPath)に変更 NSIndexPathからIndexPathに変更 PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType, subtype: PHAssetCollectionSubtype, options: PHFetchOptions?)からPHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType, subtype: PHAssetCollectionSubtype, options: PHFetchOptions?)に変更

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.