从 DKAssests 中获取选定的图像

Get selected images from DKAssests

使用 DKImagePickerController select 从图库中获取多个图像和视频。但只能从图库中获取 select 图像和视频,但无法从 DKAssets 中获取 selected 图像并保存到数组中。用了一天多。

下面是尝试的代码:

let pickerController = DKImagePickerController()


        pickerController.assetType = DKImagePickerControllerAssetType.AllAssets
        pickerController.allowsLandscape = false
        pickerController.allowMultipleTypes = true
        pickerController.sourceType = DKImagePickerControllerSourceType.Both
        pickerController.singleSelect = false

        // Clear all the selected assets if you used the picker controller as a single instance.
        //      pickerController.defaultSelectedAssets = nil

        pickerController.defaultSelectedAssets = self.assets

        pickerController.didSelectAssets = { [unowned self] (assets: [DKAsset]) in
            print("didSelectAssets")


    }
    self.presentViewController(pickerController, animated: true) {}

请指导,谢谢ion advance

试试这个功能。它对我有用。

func showImagePickerWithAssetType
    (
    assetType: DKImagePickerControllerAssetType,
    allowMultipleType: Bool,
    sourceType: DKImagePickerControllerSourceType = [.Camera, .Photo],
    allowsLandscape: Bool,
    singleSelect: Bool) {

        let pickerController = DKImagePickerController()
        pickerController.assetType = assetType
        pickerController.allowsLandscape = allowsLandscape
        pickerController.allowMultipleTypes = allowMultipleType
        pickerController.sourceType = sourceType
        pickerController.singleSelect = singleSelect
        //          pickerController.showsCancelButton = true
        //          pickerController.showsEmptyAlbums = false
        //          pickerController.defaultAssetGroup = PHAssetCollectionSubtype.SmartAlbumFavorites

        // Clear all the selected assets if you used the picker controller as a single instance.
        //          pickerController.defaultSelectedAssets = nil
        pickerController.defaultSelectedAssets = self.assets

        pickerController.didSelectAssets = { (assets: [DKAsset]) in
            print("didSelectAssets")

            self.assets = assets
            if assets.count > 0 {
                self.cameramoment.titleLabel?.textColor = UIColor.redColor()
                //self.cameramoment.titleLabel?.alpha = 50
                //self.cameramoment.titleLabel?.textColor = UIColor(red: 0.0, green: 184.0, blue: 214.0, alpha: 1.0)
            }
            else {
                //self.cameramoment.setImage( UIImage (named: "grey_camera_32x32"), forState: .Normal)
              self.cameramoment.titleLabel?.textColor = UIColor.lightGrayColor()
              self.cameramoment.titleLabel?.alpha = 50
            }
            self.collectionview.reloadData()

        }

        if UI_USER_INTERFACE_IDIOM() == .Pad {
            pickerController.modalPresentationStyle = .FormSheet;
        }

        self.presentViewController(pickerController, animated: true) {}
}

像这样调用这个函数。

self.showImagePickerWithAssetType(DKImagePickerControllerAssetType.AllPhotos, allowMultipleType: true, allowsLandscape: true, singleSelect: false)

self.assets 定义为,

var assets: [DKAsset] = []

从DKAsset获取图片。试试这个。

for asset in assets {
  asset.fetchImageWithSize(requiredImageSize, completeBlock: { image, info in
            if let img = image {
              let fixOrientationImage=img.fixOrientation()
              cell.postmomentimage1.image = fixOrientationImage
            }
    })
}
import Foundation
import UIKit
import DKImagePickerController

class addPhotoViewController: UIViewController {

var assets:[DKAsset] = []
var pickerController: DKImagePickerController!

@IBOutlet weak var selectedPhoto: UIImageView!

@IBAction func sec(_ sender: Any) {

    let pickerController = DKImagePickerController()
    pickerController.singleSelect = true

    pickerController.didSelectAssets = { (assets: [DKAsset]) in
        print("didSelectAssets")
        print(assets)
        for asset in assets {
            asset.fetchOriginalImage(true, completeBlock: { image, info in
                if let img = image {
                    let fixOrientationImage=img
                    print(fixOrientationImage)
                    print(info)
                    self.selectedPhoto.image = fixOrientationImage
                }
            })
        }
    }
    self.present(pickerController, animated: true) {}
   }
}