如何在我的应用程序中将图像标记为收藏夹
How do I mark an image as Favorite in my app
我有一组图像,每个图像在一个集合视图单元格中有三个按钮(保存、收藏、分享)。如何将图像标记为我最喜欢的图像?我想在我的应用程序内的文件夹中显示标记的图像。谢谢!
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
})
您尝试更新的 PHAsset 是一个不可变对象。请参考下面的link
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));
}];
}
我希望您使用自定义单元格来显示图像和单元格中的这 3 个按钮。
要将图像标记为收藏,最好的方法是在单元格和 collection 视图之间使用委托。
以下代码将进入您的自定义单元格,例如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
}
}
我有一组图像,每个图像在一个集合视图单元格中有三个按钮(保存、收藏、分享)。如何将图像标记为我最喜欢的图像?我想在我的应用程序内的文件夹中显示标记的图像。谢谢!
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
})
您尝试更新的 PHAsset 是一个不可变对象。请参考下面的link 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));
}];
}
我希望您使用自定义单元格来显示图像和单元格中的这 3 个按钮。 要将图像标记为收藏,最好的方法是在单元格和 collection 视图之间使用委托。
以下代码将进入您的自定义单元格,例如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
}
}