选择图片会导致重新保存到相机胶卷
Choosing a picture causes resave to camera roll
我有一个程序,用户可以在其中选择要放在屏幕上的照片,代码会自动将其放入自定义相册。但每当他们选择一张照片时,它就会将其重新保存到相机胶卷中,从而创建副本。我如何让它停止这样做?
func fetchAssetCollectionForAlbum() -> PHAssetCollection? {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
// fetch the asset for the album
let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
var picturePlaceHolder: PHObjectPlaceholder? = nil
if let _: AnyObject = collection.firstObject {
return collection.firstObject
}
return nil
}
func save(image: UIImage) {
if assetCollection == nil {
return
}
PHPhotoLibrary.shared().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
let enumeration: NSArray = [assetPlaceHolder!]
albumChangeRequest!.addAssets(enumeration)
}, completionHandler: nil)
}
我发布了一个类似的问题:
Swift 3 or 4 Saving to custom album creates duplicate images
但除了蟋蟀,我什么也没有。幸运的是,我想我找到了答案。我也会回答我自己的问题。
您的代码(与我的代码相同)是创建新资产。仅对用户用相机拍照后将图像保存到您的自定义相册有用。它适用于全新的资产。
但是,对于现有资产,您不想创建新资产。相反,您想要将现有资产添加到自定义相册。为此,您需要一种不同的方法。这是我创建的代码,它似乎可以正常工作。请记住,您必须首先获取资产 ID,以便将其发送到您的方法并访问现有资产。
因此,在您的 imagePickerController 中,您必须确定用户是选择了现有图像还是从新的相机操作中调用了该方法。
let pickerSource = picker.sourceType;
switch(pickerSource){
case .savedPhotosAlbum, .photoLibrary:
if(let url = info[UIIMagePickerControllerReferenceURL] as? NSURL{
let refURLString = refURL?.absoluteString;
/* value for refURLString looks something like assets-library://asset/asset.JPG?id=82A6E75C-EA55-4C3A-A988-4BF8C7F3F8F5&ext=JPG */
let refID = {function here to extract the id query param from the url string}
/*above gets you the asset ID, you can get the asset directly, but it is only
available in ios 11+.
*/
MYPHOTOHELPERCLASS.transferImage(toAlbum: "myalbumname", withID: refID!, ...)
}
break;
case .camera:
...
break;
}
现在,在您的照片助手 class(或任何地方的任何功能中)中,编辑资产而不是创建新资产,这就是我所拥有的。我假设可以省略 changeRequest 变量。我只是在玩,直到我做对了。通过完全荒谬的苹果文档,我至少能够注意到还有其他方法可以使用。我发现 NSFastEnumeration 参数可以是 PHAssets 的 NSArray,而不仅仅是占位符 PHObjectPlaceholder 对象。
public static func transferImage(toAlbum albumName:String, withID imageID:String, onSuccess success:@escaping(String)->Void, onFailure failure:@escaping(Error?)->Void){
guard let album = self.getAlbum(withName: albumName) else{
... failure here, albumNotFoundError
return;
}
if(self.hasImageInAlbum(withIdentifier: imageID, fromAlbum: albunName)){
... failure here, image already exists in the album, do not make another
return;
}
let theAsset = self.getExistingAsset(withLocalIdentifier: imageID);
if(theAsset == nil){
... failure, no asset for asset id
return;
}
PHPhotoLibrary.shared().performChanges({
let albumChangeRequest = PHAssetCollectionChangeRequest(for: album);
let changeRequest = PHAssetChangeRequest.init(for: theAsset!);
let enumeration:NSArray = [theAsset!];
let cnt = album.estimatedAssetCount;
if(cnt == 0){
albumChangeRequest?.addAssets(enumeration);
}else{
albumChangeRequest?.inserAssets(enumeration, at: [0]);
}
}){didSucceed, error) in
OperationQueue.main.addOperation({
didSucceed ? success(imageID) : failure(error);
})
}
}
所以,它几乎是一样的,除了不是创建资产创建请求并为创建的资产生成占位符,而是使用现有资产 ID 获取现有资产并将现有资产添加到addasset/insertasset NSArray 参数而不是新创建的资产占位符。
我有一个程序,用户可以在其中选择要放在屏幕上的照片,代码会自动将其放入自定义相册。但每当他们选择一张照片时,它就会将其重新保存到相机胶卷中,从而创建副本。我如何让它停止这样做?
func fetchAssetCollectionForAlbum() -> PHAssetCollection? {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
// fetch the asset for the album
let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
var picturePlaceHolder: PHObjectPlaceholder? = nil
if let _: AnyObject = collection.firstObject {
return collection.firstObject
}
return nil
}
func save(image: UIImage) {
if assetCollection == nil {
return
}
PHPhotoLibrary.shared().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
let enumeration: NSArray = [assetPlaceHolder!]
albumChangeRequest!.addAssets(enumeration)
}, completionHandler: nil)
}
我发布了一个类似的问题: Swift 3 or 4 Saving to custom album creates duplicate images
但除了蟋蟀,我什么也没有。幸运的是,我想我找到了答案。我也会回答我自己的问题。
您的代码(与我的代码相同)是创建新资产。仅对用户用相机拍照后将图像保存到您的自定义相册有用。它适用于全新的资产。
但是,对于现有资产,您不想创建新资产。相反,您想要将现有资产添加到自定义相册。为此,您需要一种不同的方法。这是我创建的代码,它似乎可以正常工作。请记住,您必须首先获取资产 ID,以便将其发送到您的方法并访问现有资产。
因此,在您的 imagePickerController 中,您必须确定用户是选择了现有图像还是从新的相机操作中调用了该方法。
let pickerSource = picker.sourceType;
switch(pickerSource){
case .savedPhotosAlbum, .photoLibrary:
if(let url = info[UIIMagePickerControllerReferenceURL] as? NSURL{
let refURLString = refURL?.absoluteString;
/* value for refURLString looks something like assets-library://asset/asset.JPG?id=82A6E75C-EA55-4C3A-A988-4BF8C7F3F8F5&ext=JPG */
let refID = {function here to extract the id query param from the url string}
/*above gets you the asset ID, you can get the asset directly, but it is only
available in ios 11+.
*/
MYPHOTOHELPERCLASS.transferImage(toAlbum: "myalbumname", withID: refID!, ...)
}
break;
case .camera:
...
break;
}
现在,在您的照片助手 class(或任何地方的任何功能中)中,编辑资产而不是创建新资产,这就是我所拥有的。我假设可以省略 changeRequest 变量。我只是在玩,直到我做对了。通过完全荒谬的苹果文档,我至少能够注意到还有其他方法可以使用。我发现 NSFastEnumeration 参数可以是 PHAssets 的 NSArray,而不仅仅是占位符 PHObjectPlaceholder 对象。
public static func transferImage(toAlbum albumName:String, withID imageID:String, onSuccess success:@escaping(String)->Void, onFailure failure:@escaping(Error?)->Void){
guard let album = self.getAlbum(withName: albumName) else{
... failure here, albumNotFoundError
return;
}
if(self.hasImageInAlbum(withIdentifier: imageID, fromAlbum: albunName)){
... failure here, image already exists in the album, do not make another
return;
}
let theAsset = self.getExistingAsset(withLocalIdentifier: imageID);
if(theAsset == nil){
... failure, no asset for asset id
return;
}
PHPhotoLibrary.shared().performChanges({
let albumChangeRequest = PHAssetCollectionChangeRequest(for: album);
let changeRequest = PHAssetChangeRequest.init(for: theAsset!);
let enumeration:NSArray = [theAsset!];
let cnt = album.estimatedAssetCount;
if(cnt == 0){
albumChangeRequest?.addAssets(enumeration);
}else{
albumChangeRequest?.inserAssets(enumeration, at: [0]);
}
}){didSucceed, error) in
OperationQueue.main.addOperation({
didSucceed ? success(imageID) : failure(error);
})
}
}
所以,它几乎是一样的,除了不是创建资产创建请求并为创建的资产生成占位符,而是使用现有资产 ID 获取现有资产并将现有资产添加到addasset/insertasset NSArray 参数而不是新创建的资产占位符。