iT邦幫忙

2024 iThome 鐵人賽

DAY 4
0
Mobile Development

手機Swift性能專家系列 第 4

開啟iOS相簿的方法

  • 分享至 

  • xImage
  •  

今天介紹如何開啟手機的相簿使用的照片

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    func presentImagePicker() {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePicker = UIImagePickerController()
            imagePicker.sourceType = .photoLibrary
            imagePicker.delegate = self
            present(imagePicker, animated: true, completion: nil)
        }
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let pickedImage = info[.originalImage] as? UIImage {
            // 獲取到圖片
            // 這裡可以將圖片顯示在 UI 中或進行其他處理
        }
        dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
}

透過選取相簿裡的照片可以跟非常多東西互動可以動手玩看看


上一篇
Swift 相機使用
下一篇
手機地圖使用基本介紹
系列文
手機Swift性能專家20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言