【问题标题】:Swift Can load the path as soon as I save the photo? in iOSSwift 保存照片后可以立即加载路径吗?在 iOS 中
【发布时间】:2021-11-25 07:30:06
【问题描述】:

https://developers.facebook.com/docs/instagram/sharing-to-feed

我想使用一种方法将我在应用程序中创建的照片分享到 Instagram。

但是,您可以直接分享为 Instagram 故事,但我不知道如何调出如上图所示的 Instagram 供稿和故事选择屏幕。

Post To Instagram screen on iOS Swift

我在上面的地址找到了和我有同样问题的朋友。

正确的答案是获取照片的路径并将其发送到 Instagram。

因此,我们继续保存在应用程序中创建的 UIImage。

但是,我知道如何通过选择已保存的图像来检索地址,但我不知道如何在保存后立即自动检索地址。

我通过 'UIImageWriteToSavedPhotosAlbum' 方法保存照片。但是我不知道如何从这个方法中获取路径。

我想知道是否有更新的方法,因为搜索结果已经过时并且导致很多错误并且不起作用。

或者我想知道我是否在 Instagram 上使用了错误的分享方式。

【问题讨论】:

    标签: ios swift instagram


    【解决方案1】:

    好吧,我想你想知道“如何获取最新照片?”依你的意见。 我的操作分三步,首先要监听图片库的变化,其次要实现回调事件的委托方法,最后判断是“NEW ADD”还是“DELETE”,代码如下:

    import UIKit
    import Photos
     
    class ViewController: UIViewController {
        
        // Get sources to store PHAsset/
        var assetsFetchResults:PHFetchResult<PHAsset>!
        
        // Photo manager instance with cache.
        var imageManager:PHCachingImageManager!
        
        // To show image.
        var imageView: UIImageView!
        
        // Thumb size.
        var assetGridThumbnailSize:CGSize!
        
        override func viewDidLoad() {
            
            imageView = UIImageView()
            imageView.frame = CGRect(x:20, y:40, width:100, height:100)
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
            self.view.addSubview(imageView)
            
            // Load and resize.
            self.imageManager = PHCachingImageManager()
            
            // To caculate the size of thumb.
            let scale = UIScreen.main.scale
            assetGridThumbnailSize = CGSize(width: imageView.frame.width*scale ,
                                            height: imageView.frame.height*scale)
            
            // Apply privicy.
            PHPhotoLibrary.requestAuthorization({ (status) in
                if status != .authorized {
                    return
                }
                
                // Get all photo sources before launching.
                let allPhotosOptions = PHFetchOptions()
                allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",
                                                                     ascending: false)]
                allPhotosOptions.predicate = NSPredicate(format: "mediaType = %d",
                                                         PHAssetMediaType.image.rawValue)
                self.assetsFetchResults = PHAsset.fetchAssets(with: .image,
                                                              options: allPhotosOptions)
                print("--- Soure get ok. ---")
                
                // Listen sources's change.
                PHPhotoLibrary.shared().register(self)
            })
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    }
    
    
     extension ViewController:PHPhotoLibraryChangeObserver{
        
        // Changed when Photo Library opearion.
        func photoLibraryDidChange(_ changeInstance: PHChange) {
            guard let collectionChanges = changeInstance.changeDetails(for:
                                                                        self.assetsFetchResults as! PHFetchResult<PHObject>) else { return }
            
            DispatchQueue.main.async {
                // Get all newest data.
                if let allResult = collectionChanges.fetchResultAfterChanges
                    as? PHFetchResult<PHAsset>{
                    self.assetsFetchResults = allResult
                }
                
                if !collectionChanges.hasIncrementalChanges || collectionChanges.hasMoves{
                    return
                }else{
                    print("--- Changing. ---")
                    
                    // Photo Delete.
                    if let removedIndexes = collectionChanges.removedIndexes,
                       removedIndexes.count > 0{
                        print("Delete \(removedIndexes.count)")
                    }
                    // Photo Modify.
                    if let changedIndexes = collectionChanges.changedIndexes,
                       changedIndexes.count > 0{
                        print("Modify \(changedIndexes.count)")
                    }
                    // Photo Add.
                    if let insertedIndexes = collectionChanges.insertedIndexes,
                       insertedIndexes.count > 0{
                        print("Add \(insertedIndexes.count)")
                        
                        // Get last photo.
                        let asset = self.assetsFetchResults[insertedIndexes.first!]
                        // Get thumb.
                        self.imageManager.requestImage(for: asset,
                                                       targetSize: self.assetGridThumbnailSize,
                                                       contentMode: .aspectFill, options: nil) {
                            (image, nfo) in
                            self.imageView.image = image
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      相关资源
      最近更新 更多