Modify/Edit PhAsset 图像裁剪
Modify/Edit PhAsset Image by Cropping
有什么方法可以通过裁剪或编辑来 modify/edit 相位图像吗?
我有一组资产,我想通过从选定资产获取图像并将其传递给裁剪控制器来裁剪资产图像,并且在 return 中想要更改选定资产中的裁剪图像。
有我的代码很好理解
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)
}
}
}
}
只需传递 cropped/modified 图片并获得具有相同 cropped/modified 图片的新资产
有什么方法可以通过裁剪或编辑来 modify/edit 相位图像吗? 我有一组资产,我想通过从选定资产获取图像并将其传递给裁剪控制器来裁剪资产图像,并且在 return 中想要更改选定资产中的裁剪图像。
有我的代码很好理解
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)
}
}
}
}
只需传递 cropped/modified 图片并获得具有相同 cropped/modified 图片的新资产