ios - PHPhotoLibrary 在获取 placeholderForCreatedAsset 时崩溃

标签 ios objective-c xcode ios10 phphotolibrary

我尝试使用此代码将图像放入自定义相册:

PHAssetCollection *album = [self getMyAlbum];
UIImage *image = [self getMyImage];

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];

    PHObjectPlaceholder * placeHolder = createAssetRequest.placeholderForCreatedAsset;

    PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:album];

    if(placeHolder){
        [albumChangeRequest addAssets:@[ placeHolder ]];
    }

} completionHandler:^(BOOL success, NSError *error) {
    //doesen't matter
}];

因此,我在这一行的用户日志中收到很多错误 createAssetRequest.placeholderForCreatedAsset

喜欢

1 CoreFoundation __exceptionPreprocess + 1245624

2 libobjc.A.dylib objc_exception_throw + 34136

3 Photos __48-[PHChangeRequestHelper generateUUIDIfNecessary]_block_invoke + 116552

2 libdispatch.dylib _dispatch_semaphore_wait_slow + 79828

3 Photos -[PHChangeRequestHelper generateUUIDIfNecessary] + 115992

4 Photos -[PHAssetCreationRequest placeholderForCreatedAsset] + 244020

所以 [PHChangeRequestHelper generateUUIDIfNecessary] 让我崩溃。

我只在 iOS > 10 上看到它,我无法在模拟器中重现它。

这是什么?如何解决?

最佳答案

尝试使用它。希望这就是您所寻求的。

import UIKit
import Photos

protocol CameraRollViewControllerProtocol: class {
    func displayTappedImage(displayImage: UIImage)
    func scrollToCameraButtonTapped()
}

class CameraRollViewController:  UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet var collectionView: UICollectionView!

    var images = [PHAsset]()
    var displayImage: UIImage!
    // used to specify the target size of the thumbnails of images in cameraRoll
    // In iPhone6 images of target size 100.0 were successfully obtained and in iPhone 6plus images of 400.0 were obtained successfully; for other target size a nil image was obtained.
    var thumbImageSize: Double!
    var targetSize: CGSize!
    weak var delegate: CameraRollViewControllerProtocol!

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.contentInset = UIEdgeInsets(top: 24.0, left: 24.0, bottom: 24.0, right: 24.0)
//        if UIScreen.main.bounds.width > 375 {
//            thumbImageSize = 100.0
//        } else {
            thumbImageSize = 100.0
//        }

    }

    override func viewWillAppear(_ animated: Bool) {

        collectionView.isUserInteractionEnabled = true

        self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.foregroundColor: UIColor.cameraRollNavigationBarTitle, NSAttributedStringKey.font: UIFont.gothamRoundedMedium20!]

        checkPermission { (success) in
            if success {
                OperationQueue.main.addOperation({
                    self.getImages()
                })
            }
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    // MARK: - UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {            
            if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "galleryPhotoCell", for: indexPath) as? CameraRollCell {

                let asset = images[indexPath.row]
                let manager = PHImageManager.default()

                if cell.tag != 0 {
                    manager.cancelImageRequest(PHImageRequestID(cell.tag))
                }

                cell.tag = Int(manager.requestImage(for: asset,
                                                    targetSize: CGSize(width: thumbImageSize, height: thumbImageSize),
                                                    contentMode: .aspectFill,
                                                    options: nil) { (result, _) in


                                                        if result != nil {
                                                            cell.imageView?.image = result
                                                        } else {
                                                            cell.imageView.image = #imageLiteral(resourceName: "cloud-Image")
                                                        }
                })
                return cell
            }
            else {
                return UICollectionViewCell()
            }
    }

    @IBAction func CameraButtonTapped(_ sender: Any) {
        delegate.scrollToCameraButtonTapped()
    }
}

extension CameraRollViewController {

    func getImages() {

        if images.count > 0 {
            images.removeAll()
        }

        let assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil) //fetchOptions)//nil)

        assets.enumerateObjects({ (object, count, stop) in
            self.images.append(object)
        })
        self.images.reverse()
        self.collectionView.reloadData()
    }

    func checkPermission( completionHandler:@escaping (_ success:Bool)->()) {

        PHPhotoLibrary.requestAuthorization({ (status:PHAuthorizationStatus) in
            if status == .authorized {
                completionHandler(true)
            } else {
                completionHandler(false)
            }
        })
    }
}

extension CameraRollViewController {

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 96.0, height: 96.0)
    }

    func collectionView(_ collectionView: UICollectionView, layout
        collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 20.0
    }
}

extension CameraRollViewController {

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        // check size of Asset before loading it

        let asset = images[indexPath.row]
        let resources = PHAssetResource.assetResources(for: asset)

        var sizeOnDisk: Int64? = 0

        if let resource = resources.first {
            let unsignedInt64 = resource.value(forKey: "fileSize") as? CLong
            sizeOnDisk = Int64(bitPattern: UInt64(unsignedInt64!))
        }

        LogManager.logInfo("SIZE OF IMAGE: \(converByteToHumanReadable(sizeOnDisk!))")

        let imageSizeString = sizeOnDisk!/1048576
        LogManager.logInfo(" SIZE OF IMAGE (1048576): \(imageSizeString)")

        if imageSizeString > 2 {

            targetSize = CGSize.init(width: 1000.0, height: 1000.0)

        } else {
            targetSize = PHImageManagerMaximumSize
        }

        let manager = PHImageManager.default()

        let options: PHImageRequestOptions = PHImageRequestOptions()
        //options.resizeMode = .exact
        options.deliveryMode = .highQualityFormat //.opportunistic
        //options.resizeMode = .exact

        manager.requestImage(for: asset,
                             targetSize: targetSize, //CGSize(width: 375.0, height: 667.0)  ,
                             contentMode: .aspectFit,
                             options: options) { (result, info) in

                                if (info![PHImageResultIsInCloudKey] != nil) {
                                    print("\n\nIS IN CLOUD: \(info![PHImageResultIsInCloudKey] as! Bool)")
                                }

                                guard info![PHImageResultIsDegradedKey] as! Bool == false else {
                                    LogManager.logInfo("Image is degraded")
                                    return
                                }
                                if let image = result {
                                    print("\n\n*********************************==========Local Image*********************************==========\n\n")
                                    let imageData: NSData = NSData(data: UIImageJPEGRepresentation((image), 1)!)

                                    let frameImageData: NSData = NSData(data: UIImageJPEGRepresentation((image), 0.1)!)
                                    let frameImageCompressed: UIImage = UIImage(data: frameImageData as Data)!

                                    LogManager.logInfo(" SIZE OF IMAGE LOADED EARLIER : \(image.size); \(imageData.length/1048576).\(imageData.length%1048576)")
                                    LogManager.logInfo("SIZE OF IMAGE LOADED NOW : \(frameImageCompressed.size); \(frameImageData.length/1048576).\(frameImageData.length%1048576)")

                                    self.displayImage = image
                                    self.delegate.displayTappedImage(displayImage: self.displayImage)
                                } else {

//                                    if (self.getImage(asset: asset)) != nil {
//                                        self.displayImage = self.getImage(asset: asset) //image
//                                        self.delegate.displayTappedImage(displayImage: self.displayImage)

//UNCOMMENT LATER
                                    print("\n\n*********************************==========Cloud Image*********************************==========\n\n")
                                    let options = PHImageRequestOptions()
                                    options.deliveryMode = .highQualityFormat
                                    options.isSynchronous = true
                                    //options.isNetworkAccessAllowed = true
                                    options.progressHandler = { (progress, error, stop, info) in
                                        print("PROGRESS: \(progress)") //.debug("\(progress)")
                                    }
                                    options.version = PHImageRequestOptionsVersion.original

                                    PHImageManager.default().requestImage(for: asset, targetSize:  UIScreen.main.bounds.size , contentMode: .aspectFill, options: options) { (image, info) in
                                        if image !=  nil {

                                            //print(info!["PHImageResultIsInCloudKey"] as! Bool)
                                            //compressedImageData = self.compressImage(image: image!)
                                            self.displayImage = image
                                            self.delegate.displayTappedImage(displayImage: self.displayImage)
                                        } else{
                                            // request image with size 200x200
                                            print("MUST NOT EXECUTE")
                                            print("\n\n*********************************==========Local THUMBNAIL  Image*********************************==========\n\n")
                                            let options: PHImageRequestOptions = PHImageRequestOptions()
                                            //options.resizeMode = .exact
                                            options.deliveryMode = .highQualityFormat //.opportunistic
                                            //options.resizeMode = .exact

                                            PHImageManager.default().requestImage(for: asset, targetSize: CGSize.init(width: 150, height: 150), contentMode: .aspectFill, options: options) { (image, info) in
                                                if image !=  nil {

                                                    self.displayImage = image
                                                    self.delegate.displayTappedImage(displayImage: self.displayImage)
                                                }
                                            }
                                        }
                                    }
//UNCOMMENT LATER
//                                    }
//                                    else {

                                    // show popup saying download the image from icloud in this device as it's just a thumbnail
//                                    self.createAlert(title: "Photo Not Accessible", message: "Please download it from iCloud to use it.")

//                                    }

                                }
        }
        collectionView.isUserInteractionEnabled = false
    }
}

extension CameraRollViewController {

    func createAlert(title: String, message: String) {

        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action) in
            alert.dismiss(animated: true, completion: nil)
            LogManager.logInfo("OK Pressed")
            self.collectionView.isUserInteractionEnabled = true
        }))
        self.present(alert, animated: true, completion: nil)
    }

    func converByteToHumanReadable(_ bytes:Int64) -> String {

        let formatter:ByteCountFormatter = ByteCountFormatter()
        formatter.countStyle = .binary
        return formatter.string(fromByteCount: Int64(bytes))
    }






    func getImage(asset: PHAsset) -> UIImage?  {

        var assetImage: UIImage?
        let options = PHImageRequestOptions()
        options.isSynchronous = true
        options.isNetworkAccessAllowed = true

        PHImageManager.default().requestImage(for: asset, targetSize: UIScreen.main.bounds.size, contentMode: .aspectFill, options: options) { (image, info) in
            assetImage = image
        }

        return assetImage
    }

}

关于ios - PHPhotoLibrary 在获取 placeholderForCreatedAsset 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41613988/

相关文章:

ios - 使用iOS Keychain来存储API调用的 key 和 secret

ios - block 中的 NSMutableArray addObject 给出空数组

objective-c - Objective C,iOS,有哪些选项可以在 UIImageView 中的图像顶部添加一些文本?

ios - 使 UINavigationBar 更短

ruby - 更新 cocoapod 时如何避免这种 ruby​​ 非法指令错误?

ios - Cocoapods 没有集成到 Pod 文件中

ios - 图像是由 UIImagePickerControllerSourceTypeCamera 拍摄的如何将其保存在 ios 的数据库中

ios - 在 Sprite Kit 中暂停游戏

C++ 代码在以 Objective-c 为主的项目中运行速度非常慢

ios - 警报的 UILocalNotification 替代方案