2017-07-07 68 views

回答

0

我不確定你想要什麼。

你想要瞄準iOS 8嗎?

這是我如何獲取照片,它適用於iOS(8.0和更高版本),macOS(10.11和更高版本),tvOS(10.0和更高版本) 代碼被註釋掉它可能會產生混淆

  1. 第一個功能設置的選項,以獲取照片
  2. 第二個函數實際上將獲取它們

    //import the Photos framework 
    import Photos 
    //in these arrays I store my images and assets 
    var images = [UIImage]() 
    var assets = [PHAsset]() 
    
    fileprivate func setPhotoOptions() -> PHFetchOptions{ 
         let fetchOptions = PHFetchOptions() 
         fetchOptions.fetchLimit = 15 
         let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false) 
         fetchOptions.sortDescriptors = [sortDescriptor] 
         return fetchOptions 
        } 
    
    
    
    
    fileprivate func fetchPhotos() { 
    let allPhotos = PHAsset.fetchAssets(with: .image, options: setPhotoOptions()) 
    
    DispatchQueue.global(qos: .background).async { 
    
        allPhotos.enumerateObjects({ (asset, count, stop) in 
         let imageManager = PHImageManager.default() 
         let targetSize = CGSize(width: 200, height: 200) 
         let options = PHImageRequestOptions() 
         options.isSynchronous = true 
         imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { (image, info) in 
    
          if let image = image { 
           self.images.append(image) 
           self.assets.append(asset) 
    
    
          } 
    
          if count == allPhotos.count - 1 { 
           DispatchQueue.main.async { 
            //basically, here you can do what you want 
            //(after you finish retrieving your assets) 
            //I am reloading my collection view 
            self.collectionView?.reloadData() 
           } 
          } 
    
         }) 
    
    
        }) 
    
    } 
    

    }

根據OP的澄清編輯

您需要設置委託UIImagePickerControllerDelegate

然後實現以下功能

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

中所述方法,得到這樣的形象:

var image : UIImage = info[UIImagePickerControllerEditedImage] as! UIImage 
+0

我想要從我從的UIImagePickerController –

+0

得到的圖像我編輯我的答案 –

+0

的PHAsset我不能相信這樣做的唯一途徑是獲取整個照片庫,然後枚舉每一張照片! –

1

我有同樣的問題,首先檢查權限並請求訪問:

let status = PHPhotoLibrary.authorizationStatus() 

if status == .notDetermined { 
    PHPhotoLibrary.requestAuthorization({status in 

    }) 
} 

只需掛鉤即可觸發您的UIImagePickerController。委託調用現在應該在userInfo中包含PHAsset。

guard let asset = info[UIImagePickerControllerPHAsset] as? PHAsset