iPhone 6 iPhone 6 - 2 months ago 21
iOS Question

Get selected images from DKAssests

Using DKImagePickerController from selecting multiple images and videos from gallery. But only able to select images and videos from gallery, yet not able to get selected images from DKAssets and save to array. Spent more than a day.

Below is the code trying:

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) {}


Please guide, thanks ion advance.

Answer

Try this function. it worked for me.

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 = { [unowned self] (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) {}
}

Call this function like this.

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

self.assets is defines as,

var assets: [DKAsset] = []

To get the image from DKAsset. try this.

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