Ios 当图像的方向不同时,为什么图片动画效果不同

Ios 当图像的方向不同时,为什么图片动画效果不同,ios,swift,Ios,Swift,我面临一个大问题。。我只是不知道为什么它不能正常工作。 我正在制作一个图库应用程序,希望在图库和全屏图像之间进行平滑过渡 我觉得我的动画效果很好。。但是我拍摄了一张不同方向的照片,我的动画不再工作了。 你知道为什么我的动画依赖于图片的方向吗 这是我的工作动画 这是我的破动画 func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { guard let to

我面临一个大问题。。我只是不知道为什么它不能正常工作。 我正在制作一个图库应用程序,希望在图库和全屏图像之间进行平滑过渡

我觉得我的动画效果很好。。但是我拍摄了一张不同方向的照片,我的动画不再工作了。 你知道为什么我的动画依赖于图片的方向吗

这是我的工作动画

这是我的破动画

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    guard   let toView = transitionContext.view(forKey: .to),
            let fromVC = transitionContext.viewController(forKey: .from)?.childViewControllers.first as? NavigationGalleryViewController,
            let fromView = fromVC.collectionView?.cellForItem(at: indexPath) as? InstallationViewCell
        else {
            transitionContext.completeTransition(true)
            return
    }

    let finalFrame = toView.frame

    let viewToAnimate = UIImageView(frame: originFrame)
    viewToAnimate.image = fromView.imageView.image
    viewToAnimate.contentMode = .scaleAspectFill
    viewToAnimate.clipsToBounds = true
    fromView.imageView.isHidden = true

    let containerView = transitionContext.containerView
    containerView.addSubview(toView)
    containerView.addSubview(viewToAnimate)

    toView.isHidden = true

    // Determine the final image height based on final frame width and image aspect ratio
    let imageAspectRatio = viewToAnimate.image!.size.width / viewToAnimate.image!.size.height
    var finalImageheight = finalFrame.width / imageAspectRatio

    if (finalImageheight > UIScreen.main.bounds.height) {
        finalImageheight = UIScreen.main.bounds.height
    }

    // Animate size and position
    UIView.animate(withDuration: duration, animations: {
        viewToAnimate.frame.size.width = finalFrame.width
        viewToAnimate.frame.size.height = finalImageheight
        viewToAnimate.center = CGPoint(x: finalFrame.midX, y: finalFrame.midY)
    }, completion:{ _ in
        toView.isHidden = false
        fromView.imageView.isHidden = false
        viewToAnimate.removeFromSuperview()
        transitionContext.completeTransition(true)
    })

}

正如您所看到的,高度和宽度是相同的,这不是比率问题

这是我的动画

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    guard   let toView = transitionContext.view(forKey: .to),
            let fromVC = transitionContext.viewController(forKey: .from)?.childViewControllers.first as? NavigationGalleryViewController,
            let fromView = fromVC.collectionView?.cellForItem(at: indexPath) as? InstallationViewCell
        else {
            transitionContext.completeTransition(true)
            return
    }

    let finalFrame = toView.frame

    let viewToAnimate = UIImageView(frame: originFrame)
    viewToAnimate.image = fromView.imageView.image
    viewToAnimate.contentMode = .scaleAspectFill
    viewToAnimate.clipsToBounds = true
    fromView.imageView.isHidden = true

    let containerView = transitionContext.containerView
    containerView.addSubview(toView)
    containerView.addSubview(viewToAnimate)

    toView.isHidden = true

    // Determine the final image height based on final frame width and image aspect ratio
    let imageAspectRatio = viewToAnimate.image!.size.width / viewToAnimate.image!.size.height
    var finalImageheight = finalFrame.width / imageAspectRatio

    if (finalImageheight > UIScreen.main.bounds.height) {
        finalImageheight = UIScreen.main.bounds.height
    }

    // Animate size and position
    UIView.animate(withDuration: duration, animations: {
        viewToAnimate.frame.size.width = finalFrame.width
        viewToAnimate.frame.size.height = finalImageheight
        viewToAnimate.center = CGPoint(x: finalFrame.midX, y: finalFrame.midY)
    }, completion:{ _ in
        toView.isHidden = false
        fromView.imageView.isHidden = false
        viewToAnimate.removeFromSuperview()
        transitionContext.completeTransition(true)
    })

}
我所有的帧都很好,我检查了4次,我的开始帧和结束帧都很好


关于图片、方向或动画,有什么我应该知道的吗

UIImage方向是一个很大的错误。最简单的方法是将ImageView中的所有UIImage实例规范化为一个方向。如果在

中有很好的描述,它是如何完成的?是否检查viewToAnimate.frame是否与采集单元格本身不是相对帧?因为如果X等于214,它就出界了。工作动画的帧参数是什么,以及“originFrame”的值是什么?请尝试设置
viewToAnimate.frame=fromView.superview!。convert(fromView.frame,to:transitionContext.containerView)
如何在此语句中设置
originFrame
让viewToAnimate=UIImageView(frame:originFrame)
?看到了吗?实际上,更改方向确实解决了我的问题,但这不是一个解决方案,而是一个旁路。。但至少我知道问题出在方向上