【问题标题】:Get selected images from DKAssests从 DKAssests 中获取选定的图像
【发布时间】:2016-09-19 10:52:14
【问题描述】:

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

下面是代码尝试:

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

请指导,谢谢离子提前。

【问题讨论】:

  • 到目前为止你做了什么?
  • 您必须及时响应。以便您得到答案。
  • 张贴在我迄今为止尝试过的内容之上,完成了很多谷歌搜索。还分享了我试图关注的链接。 github.com/zhangao0086/DKImagePickerController/issues/122
  • 在下面发布了答案。检查一下,如果您遇到任何问题,请告诉我

标签: ios iphone swift uiimagepickercontroller


【解决方案1】:

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

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

【讨论】:

  • self.assets = assets 崩溃
  • 您选择了一张照片吗?因为它可能会返回 nil
  • 将 self.assets = assets 放入 if 条件中
  • 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) print("count more than 0") self.assets = assets }
【解决方案2】:
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) {}
   }
}

【讨论】:

    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 2018-08-16
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 2011-07-23
    相关资源
    最近更新 更多