Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotating an image #1

Open
orta opened this issue Dec 13, 2014 · 2 comments
Open

Rotating an image #1

orta opened this issue Dec 13, 2014 · 2 comments

Comments

@orta
Copy link

orta commented Dec 13, 2014

I expect a lot of people will be interested in this, so I'm putting up my example code. This uses core image to rotate the image inside an imageview and records the rotation into a animation.

This is because I couldn't get a transform to work on my snapshots how I would normally do it:

        let number = CGFloat(-1.01 * M_PI)
        let rotationTransform = CATransform3DMakeRotation(number, 0, 0, 1)
        let rotationAnimation = CABasicAnimation(keyPath: "transform")

        rotationAnimation.toValue = NSValue(CATransform3D: rotationTransform);
        rotationAnimation.duration = 0.4
        rotationAnimation.cumulative = true
        rotationAnimation.repeatCount = 10000;

        let image = UIImage(named: "Spinner-0");
        let imageView = UIImageView(image: image);
        imageView.layer.addAnimation(rotationAnimation, forKey:"transform")

        let outer = UIView(frame: imageView.bounds)
        outer.addSubview(imageView)

        view.addSubview(outer)
        arcFlipbook = Flipbook()

        arcFlipbook.renderTargetView(outer, imagePrefix: "Progress-Spinner", frameCount: 20) { (view, frame) in
            return
        }

Zip of my working example using CIImage: http://cl.ly/3H20450t1T0F/Flipbook-orta.zip

@rivera-ernesto
Copy link

I have a similar problem there I modify the view's transform inside the updateBlock, but because the UI loop is not running no "visible" changes are captured by Flipbook.

I tried using dispatch_async with no luck:

for frame in 0..<frameCount {
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                updateBlock(view: view, frame: frame)
                self.renderViewToImage(view)
            })
        }

frosty pushed a commit that referenced this issue Sep 8, 2015

Verified

This commit was signed with the committer’s verified signature.
neo1973 Markus Härer
Fix to work with latest Xcode / Swift
@elbelga
Copy link

elbelga commented Mar 22, 2016

I have made some changes in Flipbook class to make this, I'm sure there are better ways to do this but it works ^_^

I added 3 new private properties

private lazy var displayLinkByFrameCount: CADisplayLink = CADisplayLink(target: self, selector: "displayLinkTickByFrameCount:")
private var frameCount = 0
private var updateBlock: ((view: UIView, frame: Int) -> Void)?

I have modified the method func renderTargetView(view: UIView, imagePrefix: String, frameCount: Int, updateBlock: (view: UIView, frame: Int) -> Void)

func renderTargetView(view: UIView, imagePrefix: String, frameCount: Int, updateBlock: (view: UIView, frame: Int) -> Void) {
        self.targetView = view
        self.imagePrefix = imagePrefix
        self.frameCount = frameCount
        self.updateBlock = updateBlock
        self.imageCounter = 0
        displayLinkByFrameCount.frameInterval = 1
        displayLinkByFrameCount.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)

        print("[Flipbook] Starting capture...")
    }

And I added this new method

    func displayLinkTickByFrameCount(sender: CADisplayLink) {

        renderViewToImage(self.targetView)

        if self.imageCounter > self.frameCount  {
            sender.invalidate()
            sender.removeFromRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)

            print("[Flipbook] Images exported to: \(documentsDirectory()!)")
            print("[Flipbook] Capture complete!")
        } else {
            self.updateBlock!(view: self.targetView!, frame: self.imageCounter)
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants