如何在ios UIPageViewController中加载Image

时间:2016-05-18 16:01:21

标签: ios asynchronous photo uipageviewcontroller

$ java -cp semiotics-alpha-1.zap org.eclipse.core.runtime.adaptor.EclipseStarter -console
osgi> exit
Really want to stop Equinox? (y/n; default=y)  y

func viewPhotoDetalController(index: Int) -> PhotoDetailViewController? { if let storyboard = storyboard, detailController = storyboard.instantiateViewControllerWithIdentifier("PhotoDetail") as? PhotoDetailViewController { //loadPhoto() return detailController } return nil } 中向左或向右滑动时,会调用上面的代码, 在返回detailController之前我想加载照片并像这样设置detailController的照片变量

UIPageViewController

但问题是func loadPhoto (index:Int) { let imgManager = PHCachingImageManager() imgManager.requestImageForAsset(self.devicePhotosAsset[self.index] as! PHAsset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: nil, resultHandler: {(result, info) -> Void in // set the detailController's photo here }) } 是一个异步api,当照片加载时返回detailController。如果无效,还有requestImageForAsset api的返回类型,所以我不能做类似

的事情
requestImageForAsset

那么如何设置detailController的照片变量?

这里是detailController的完整代码,我按照UIScrollView教程here

imgManager.requestImageForAsset(self.devicePhotosAsset[self.index] as! PHAsset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: nil, resultHandler: {(result, info) -> Void in
            detailController.photo = result
            return detailController

        })

2 个答案:

答案 0 :(得分:0)

将PHAsset传递给PhotoDetailViewController,并在其viewdidload中调用以下载图像

 imgManager.requestImageForAsset(phasset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: nil, resultHandler: {(result, info) -> Void in
                self.photo = result

            })

如果他们有任何问题,请告诉我

答案 1 :(得分:0)

尝试使用闭包,因为clouser捕获定义它的上下文的变量和常量。所以使用闭包你可以解决你的问题

func viewPhotoDetalController(index: Int) -> PhotoDetailViewController? {
    if let storyboard = storyboard,
        detailController = storyboard.instantiateViewControllerWithIdentifier("PhotoDetail")
            as? PhotoDetailViewController {
        loadPhoto(2, completionHandler: {[weak self] (image) in
            detailController.imageView.image = image
        })
        return detailController
    }
    return nil
}

和loadPhoto方法将如下所示

func loadPhoto(index:Int,completionHandler:(image:UIImage?)->())  {
    let imgManager = PHCachingImageManager()

    imgManager.requestImageForAsset(self.devicePhotosAsset[self.index] as! PHAsset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: nil, resultHandler: {(result, info) -> Void in
        completionHandler(image: result)
    })
}