iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 9
1
Software Development

Swift 學習目標 -- 30 天送審第一支APP系列 第 9

DAY 9: 如何把裝置的 Image 傳送進 APP

class ViewController: UICollectionViewController {
    

    func grabIMG() {
        let imageManager = PHImageManager.default()
        let requestOption = PHImageRequestOptions()
        //this is the msg we sending, asking what kinda IMG we're grabing
        requestOption.isSynchronous = true
        //do you want all IMGs coming in at once?
        requestOption.deliveryMode = .highQualityFormat
        fetchOption.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: 
        false)]
    }
}

isSynchronous => 意思是說:是否目前只回傳單一的 image,並阻擋載入其他的 image ,直到其他 image 已經可供載入。(是一個預設為 false 的 Bool)
deliveryMode => 指定要求的 image 品質:有 FastFormat:快速模式 ; HighqualityFormat 一次只嘗試 load 一個 image ,為高品質模式 ; Opportunistic 介於兩者之間,一次可能嘗試 load 一張或不只一張 image ,載入速度與品質的平衡。
.sortDescriptors => 我們希望匯入的 image 如何被歸類。 key: "creationDate" 指的是依照照片創造的日期,ascending: 指的是依照舊到新排序。
*NSSortDescriptor 外圍需要加上 [],因為 NSSortDescriptor 本身的設計是一個 array 結構,允許下不只一組 sort 方法。

接著,著手繼續實現 UICollectionViewController 裡面該有的值:

首先,定義一個名為 imageArray 的 UIImage array ,儲存 image。

var imageArray = [UIImage]()
...

    override func collectionView(collectionView: UICollectionView, 
        numberOfItemsInSection: Int) -> Int{
            return imageArray.count
        }

並定義裡面該有 image cell 的繼承方法及內容

    override func collectionView(collectionView: UICollectionView, 
        cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", 
            for: indexPath as IndexPath)
            let imageView = cell.viewWithTag(1) as! UIImageView
            imageView.image = imageArray[indexPath.row]
            
            return cell
        }

完成之後,記得再回到整個 class ViewController: UICollectionViewController 的一開始,呼叫我們定義了很久的 grabIMG() function 。

    override func viewDidLoad() {
        grabIMG()
    }

完成!!


上一篇
DAY 8: 取得 iOS Developer License
下一篇
DAY 10: 淺談 Swift 的錯誤處理
系列文
Swift 學習目標 -- 30 天送審第一支APP33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言