0

I have an array of images and I have three buttons ( Save, Favorite, Share) each image in a collectionviewcell. How do I mark an image as my favorite image? And I would like to display the marked images in a folder inside my app. Thank you!

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

                    })

NicolasElPapu
  • 1,612
  • 2
  • 11
  • 26
NelsonDharu
  • 63
  • 1
  • 7

2 Answers2

2

The PHAsset you are trying to update is an immutable object. Please refer to the link below https://developer.apple.com/documentation/photokit/phasset

To mark the asset as private you need to create a PHAssetChange request within the photo change execution block. This information is already provided on apple developer webpage. Here is the block of code as specified on the apple documentation - 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));
    }];
}
Akhilesh Sharma
  • 1,580
  • 1
  • 17
  • 29
-1

I hope you are using a custom cell for displaying the images and these 3 buttons in the cell. For marking the image as favourite, best way is to use delegation between cell and collection view.

Following code will go in your custom cell, e.g. 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()
}
}

Following code will go in your view controller having collectionView implementation, e.g. 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 
        }

}
grow4gaurav
  • 3,145
  • 1
  • 11
  • 12