gpt4 book ai didi

ios - 选择图片会导致重新保存到相机胶卷

转载 作者:行者123 更新时间:2023-11-30 12:18:43 24 4
gpt4 key购买 nike

我有一个程序,用户可以选择要放在屏幕上的照片,代码会自动将其放入自定义相册中。但每当他们选择一张照片时,它就会将其重新保存到相机胶卷中,从而创建重复项。我怎样才能让它停止这样做?

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

但我除了蟋蟀之外什么也没有。幸运的是,我想我找到了答案。我也会回答我自己的问题。

您拥有的代码(与我拥有的代码相同)是创建新 Assets 。它仅适用于用户使用相机拍照后将图像保存到自定义相册。它适用于全新 Assets 。

但是,对于现有 Assets ,您不想创建新 Assets 。相反,您希望将现有 Assets 添加到自定义相册中。为此,您需要一种不同的方法。这是我创建的代码,它似乎可以工作。请记住,您必须首先获取 Assets ID,以便您可以将其发送到您的方法并访问现有 Assets 。

因此,在 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;
}

现在,在您的 photohelper 类中(或在任何地方的任何函数中,无论什么),编辑 Assets 而不是创建新 Assets ,这就是我所拥有的。我假设可以省略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);
})
}

}

因此,它几乎是相同的,只不过不是创建 Assets 创建请求并为创建的 Assets 生成占位符,而是仅使用现有 Assets ID 来获取现有 Assets 并将现有 Assets 添加到添加 Assets /insertasset NSArray 参数而不是新创建的资源占位符。

关于ios - 选择图片会导致重新保存到相机胶卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45083741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com