通过裁剪修改/编辑PhAsset图像
Posted
技术标签:
【中文标题】通过裁剪修改/编辑PhAsset图像【英文标题】:Modify/Edit PhAsset Image by Cropping 【发布时间】:2019-09-30 12:49:34 【问题描述】:有没有办法通过裁剪或编辑来修改/编辑相集图像? 我有资产数组,我想通过从选定资产中获取图像并将其传递给裁剪控制器来裁剪资产图像,作为回报,我想更改选定资产中的裁剪图像。
有我的代码好理解
func presentCropViewController(with:IndexPath)
self.allPhotos[with.item].getImage (img) in
if let image = img
self.indexPathForCropping = with
let cropViewController = CropViewController(image: image)
cropViewController.delegate = self
cropViewController.view.tintColor = UIColor.themeGreen()
self.present(cropViewController, animated: true, completion: nil)
从资产传递图像后,我使用此方法得到裁剪图像
func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int)
cropViewController.dismiss(animated: true, completion: nil)
// Here i get the cropped image and want to update selected asset with this image
如果您也提到投票理由,我将不胜感激,因此我会相应地准备我的问题
【问题讨论】:
【参考方案1】:我发现解决方案可能不是一种有效的方法,但解决了我的问题
extension PHAsset
func updateChanges(with img:UIImage,completion:@escaping(PHAsset?)->())
phphotoLibrary.shared().performChanges(
// create cropped image into phphotolibrary
PHAssetChangeRequest.creationRequestForAsset(from: img)
) (success, error) in
if success
// fetch request to get last created asset
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: false)]
fetchOptions.fetchLimit = 1
let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
if let asset = fetchResult.firstObject
// replace your selected asset with new cropped one
completion(asset)
else
completion(nil)
else
completion(nil)
只需传递裁剪/修改后的图像并获得具有相同裁剪/修改后图像的新资产
【讨论】:
以上是关于通过裁剪修改/编辑PhAsset图像的主要内容,如果未能解决你的问题,请参考以下文章
使用本地图像 URL 或 UIIMage 获取 PHAsset?