【问题标题】:Displaying thumbnail after photo shoot in Swift 2在 Swift 2 中拍摄照片后显示缩略图
【发布时间】:2015-12-23 21:06:43
【问题描述】:

当尝试加载和显示刚刚拍摄的照片的缩略图时:

func photom(){

    if let videoConnection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {

        self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) { (imageDataSampleBuffer, error) -> Void in

            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
            let lPhoto = UIImage(data: imageData)
            var imageIdentifier: String?

            PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in

                let changeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(lPhoto!)
                let placeHolder = changeRequest.placeholderForCreatedAsset
                imageIdentifier = placeHolder!.localIdentifier

            }, completionHandler: { success, error in

                if success {

                    self.currentSelectedID = imageIdentifier!
                    self.makeSelectedItemButton(0)

                    return
                }
            })
        }
    }
}

ma​​keSelectedItemButton() 是:

func makeSelectedItemButton(layConst: CGFloat){

    if let selButto = view.viewWithTag(6){
        selButto.removeFromSuperview()
    }

    PhotoHelper().getThumbnailWithIdentifer(currentSelectedID, groesse: CGSize(width: cellHocR ,height: cellHocR), completion: { (image) -> Void in

        if image != nil {
            let libView = UIImageView()

            libView.frame = CGRectMake(self.abstanR, self.abstanR, 22, 22)
            libView.layer.cornerRadius = 11
            libView.clipsToBounds = true
            libView.contentMode = .ScaleAspectFill
            libView.image = image

            let libButton = UIButton()
            libButton.backgroundColor = UIColor.clearColor()
            libButton.tag = 6
            libButton.frame = CGRectMake(0+layConst, self.view.frame.height-self.cellHocR, self.cellHocR, self.cellHocR)
            libButton.addTarget(self, action: "libTap:", forControlEvents: .TouchUpInside)
            libButton.insertSubview(libView, atIndex: 0)
            self.view.addSubview(libButton)

        }
    })
}

PhotoHelper().getThumbnailWithIdentifer 是:

func getThumbnailWithIdentifer(localIdentifier:String, groesse: CGSize, completion: (image:UIImage?) -> Void) {

    let manager = PHImageManager.defaultManager()
    let fetchOptions = PHFetchOptions()
    fetchOptions.predicate = NSPredicate(format: "mediaType == %d", PHAssetMediaType.Image.rawValue)

    let fetchResults = PHAsset.fetchAssetsWithLocalIdentifiers([localIdentifier], options: fetchOptions)

    if fetchResults.count > 0 {

        if let imageAsset = fetchResults.objectAtIndex(0) as? PHAsset {

            let requestOptions = PHImageRequestOptions()
            requestOptions.synchronous = true
            requestOptions.deliveryMode = .HighQualityFormat
            manager.requestImageForAsset(imageAsset, targetSize: groesse, contentMode: .AspectFit, options: requestOptions, resultHandler: { (image, info) -> Void in
                completion(image: image)
                print(image!.size)
            })

        } else {

            completion(image: nil)
        }

    } else {

        completion(image: nil)
    }
}

这会使按钮在照片制作后至少显示 5 秒。使用旧照片执行时,相同的功能可以快速且按预期工作。这里有什么问题?

【问题讨论】:

    标签: uiview swift2 phasset phphotolibrary


    【解决方案1】:

    由于 getThumbnailWithIdentifer 中的以下行,您是否正在等待:

    requestOptions.synchronous = true
    

    您可以将其更改为false,然后您可以尝试使用占位符缩略图图像,直到实际缩略图返回?

    【讨论】:

    • 对不起,我很久以前用 dispatch_after 解决了这个问题。我不知道您的解决方案是否有效,但谢谢!
    猜你喜欢
    • 2012-12-12
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    相关资源
    最近更新 更多