Ios 从DKAssests获取所选图像
使用DKImagePickerController从gallery中选择多个图像和视频。但只能从gallery中选择图像和视频,但无法从DKAssets中获取所选图像并保存到阵列。花了一天多的时间 下面是正在尝试的代码:Ios 从DKAssests获取所选图像,ios,iphone,swift,uiimagepickercontroller,Ios,Iphone,Swift,Uiimagepickercontroller,使用DKImagePickerController从gallery中选择多个图像和视频。但只能从gallery中选择图像和视频,但无法从DKAssets中获取所选图像并保存到阵列。花了一天多的时间 下面是正在尝试的代码: let pickerController = DKImagePickerController() pickerController.assetType = DKImagePickerControllerAssetType.AllAssets
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)
自我资产定义为:
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
}
})
}
试试这个功能。这对我有用
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)
自我资产定义为:
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崩溃您选择了照片吗?因为它可能返回nilput,将self.assets=assets放在if条件if assets.count>0{//self.cameramoment.titleLabel?.textColor=UIColor.redColor()//self.cameramoment.titleLabel?.alpha=50//self.cameramoment.titleLabel?.textColor=UIColor(红色:0.0,绿色:184.0,蓝色:214.0,alpha:1.0)打印(“计数超过0”)self.assets=assets}让我们来看看。崩溃在self.assets=assets你选择了一张照片吗?因为如果assets.count>0,它可能会返回nilput将self.assets=assets放在该条件内{//self.cameramoment.titleLabel?.textColor=UIColor.redColor()//self.cameramoment.titleLabel?.alpha=50//self.cameramoment.titleLabel?.textColor=UIColor(红色:0.0,绿色:184.0,蓝色:214.0,阿尔法:1.0)打印(“计数大于0”)self.assets=assets}让我们来。