Sacha.R
Sacha.R

Reputation: 286

Why a picture animation works different when image is oriented differently

I'm facing a big issue.. And I just don't know why it does not work correctly. I'm making a gallery application and want to have smooth transition between gallery and fullscreen image.

I though my animation worked perfectly.. but I captured a picture with different orientation and my animation doesn't work anymore. Do you have any idea why my animation is dependent to the orientation of the picture ?

here is my working animation.

enter image description here

Here is my broken animation

enter image description here

As you can see the height and the width are the same, and this is not a ratio problem.

here is my animation

    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)
    })

}

All my frames are good in either of way, I checked them 4 times, my begin and ending frames are good.

Frames

Is there something I should know about pictures, orientation or animation ?

Upvotes: 3

Views: 206

Answers (1)

mas'an
mas'an

Reputation: 160

UIImage orientations is one big messy bug. The easiest way is to normalize all UIImage instances in imageViews to one orientation. How it's done if well described in this answer

Upvotes: 2

Related Questions