如何在我的应用程序中把图片标记为收藏夹?

问题描述 投票:0回答:1

我有一个图片数组,并且我有三个按钮(保存、收藏、分享),每个图片都在一个collectionviewcell中。我如何将一张图片标记为我最喜欢的图片?而且我想在我的应用程序中的文件夹中显示标记的图像。谢谢你!我有一个数组图片,我想在我的应用程序中的文件夹中显示标记的图片。

import Photos 

    @objc func favouriteImage(sender: UIButton) {
            for index in 0..<images.count {
                if sender.tag == index {

                    PHPhotoLibrary.shared().performChanges({
                        let request = PHAssetChangeRequest(forAsset: )
                        request.favorite = true
                    }, completionHandler: { success, error in

                    })

ios swift uicollectionview phphotolibrary
1个回答
0
投票

我希望你使用的是自定义单元格来显示图片和单元格中的这三个按钮。为了将图片标记为收藏夹,最好的方法是在单元格和集合视图之间使用授权。

下面的代码将进入你的自定义单元格,例如:ImageCollectionViewCell。

protocol ImageCollectionViewCellProtocol {
 func didSelect(cell: ImageCollectionViewCell)
}

class ImageCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var favouriteButton: UIButton!

override func awakeFromNib() {
    super.awakeFromNib()        
    let image = UIImage(named: "favourite1.png")
    let imageFilled = UIImage(named: "favourite2.png")
    favouriteButton.setImage(image, for: .normal)
    favouriteButton.setImage(imageFilled, for: .selected)
}

// The IBAction for Favourite button
@IBAction func didTapMakeFavourite(_ sender: Any) {
    guard let cell = (sender as AnyObject).superview?.superview as? ImageCollectionViewCell else {
        return // or fatalError() or whatever
    }

    self.delegate.didSelect(cell: cell)
    favouriteButton.isSelected.toggle()
}
}

下面的代码将在你的视图控制器中实现集合视图,例如 ImagesCollectionViewController。

extension ImagesCollectionViewController: UICollectionViewDataSource {
            func collectionView(_ collectionView: UICollectionView, 
      cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
             ... 
                cell.delegate = self
             ...
            }

    }

extension ImagesCollectionViewController: ImageCollectionViewCellProtocol {
        func didSelect(cell: ImageCollectionViewCell) {
            let indexPath = imagesCollectionView.indexPath(for: cell)
            // do whatever you want with this indexPath's cell 
        }

}

0
投票

你要更新的PHAsset是一个不可更改的对象。请参考以下链接 https:/developer.apple.comdocumentationphotokitphasset。

要将资产标记为私有,你需要在照片更改执行块中创建一个PHAssetChange请求。这个信息已经在apple developer网页上提供了。以下是apple文档中指定的代码块------。https:/developer.apple.com文件资料photokitphassetchangerequest。

- (void)toggleFavoriteForAsset:(PHAsset *)asset {
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        // Create a change request from the asset to be modified.
        PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset];
        // Set a property of the request to change the asset itself.
        request.favorite = !asset.favorite;

    } completionHandler:^(BOOL success, NSError *error) {
        NSLog(@"Finished updating asset. %@", (success ? @"Success." : error));
    }];
}
© www.soinside.com 2019 - 2024. All rights reserved.