【问题标题】:How do I mark an image as Favorite in my app如何在我的应用程序中将图像标记为收藏
【发布时间】:2020-05-10 17:10:19
【问题描述】:

我有一组图像,并且在 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】:

    您尝试更新的 PHAsset 是一个不可变对象。请参考以下链接 https://developer.apple.com/documentation/photokit/phasset

    要将资产标记为私有,您需要在照片更改执行块中创建 PHAssetChange 请求。此信息已在苹果开发者网页上提供。 这是苹果文档中指定的代码块 - https://developer.apple.com/documentation/photokit/phassetchangerequest

    - (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));
        }];
    }
    

    【讨论】:

      【解决方案2】:

      我希望您使用自定义单元格来显示单元格中的图像和这 3 个按钮。 要将图像标记为收藏,最好的方法是在单元格和集合视图之间使用委托。

      以下代码将进入您的自定义单元格,例如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()
      }
      }
      

      以下代码将进入具有 collectionView 实现的视图控制器,例如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 
              }
      
      }
      

      【讨论】:

      • @NelsonDharu 如果对您有帮助,请点赞我的回答。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 2018-06-27
      • 1970-01-01
      相关资源
      最近更新 更多