Swift 通过裁剪修改/编辑相位集图像
是否有任何方法可以通过裁剪或编辑来修改/编辑相位集图像? 我有一个资产数组,我想通过从所选资产获取图像并传递裁剪控制器来裁剪资产的图像,作为回报,我想在所选资产中更改裁剪后的图像 有我的代码需要很好地理解Swift 通过裁剪修改/编辑相位集图像,swift,xcode,phasset,photosframework,Swift,Xcode,Phasset,Photosframework,是否有任何方法可以通过裁剪或编辑来修改/编辑相位集图像? 我有一个资产数组,我想通过从所选资产获取图像并传递裁剪控制器来裁剪资产的图像,作为回报,我想在所选资产中更改裁剪后的图像 有我的代码需要很好地理解 func presentCropViewController(with:IndexPath) { self.allPhotos[with.item].getImage { (img) in if let image = img{ self.i
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
}
如果您也提到投票原因,我将不胜感激。因此,我据此准备了我的问题。我发现解决方案可能不是一种有效的方法,但解决了我的问题
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)
}
}
}
}
只需传递裁剪/修改后的图像,即可获得具有相同裁剪/修改后图像的新资源