NSImage rotation in NSView is not working

情到浓时终转凉″ 提交于 2020-01-01 19:44:48

问题


I am trying to rotate an NSImage inside a NSView., First I will show you what I have done so far.


Image :



Header file


#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

@interface AppController : NSObject {
    CALayer *syncFirst;
}

@property (weak) IBOutlet NSView *nsview;

- (IBAction)buttonClicked:(id)sender;
- (CGImageRef) convertToCGImageFromNasImage:(NSImage *) image;

@end

Implementation file, only related methods


- (void)awakeFromNib {

    NSImage *image = [NSImage imageNamed:@"SyncRing.png"];
    //init layer
    syncFirst = [CALayer layer];

    //animatated content size init
    syncFirst.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
    syncFirst.position = CGPointMake(10, 10);
    syncFirst.contents = (id)[self convertToCGImageFromNasImage:image];

    [nsview.layer addSublayer:syncFirst];
}

 /*To convert NSImage in CGImage*/

- (CGImageRef) convertToCGImageFromNasImage:(NSImage *) image {
     NSData* cocoaData = [NSBitmapImageRep TIFFRepresentationOfImageRepsInArray: [image representations]];
     CFDataRef carbonData = (__bridge CFDataRef)cocoaData;
     CGImageSourceRef imageSourceRef = CGImageSourceCreateWithData(carbonData, NULL);
     CGImageRef myCGImage = CGImageSourceCreateImageAtIndex(imageSourceRef, 0, NULL);
     return myCGImage;
}

/*When button click animate the image*/

- (IBAction) buttonClicked:(id)sender {
    CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 1];
    rotationAnimation.duration = 1.0f;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [syncFirst addAnimation:rotationAnimation forKey:@"rotateAnimation"];
}

Question :


Basically I am trying to rotate this image after click a button. The code I am trying is not 100% implementation but learnt from several different places including Apple documentation. Now, it is obvious that I am doing some naive mistake but logically my code seems correct to me. It would be great if someone can point out my mistake and give me understanding about why it is not working.


回答1:


For any future visitors, The code works perfectly and you may use it if you want except make sure before you add layer inside nsview we have to set that nsview wants layer.

so, the changes will be...


- (void)awakeFromNib {

    NSImage *image = [NSImage imageNamed:@"SyncRing.png"];
    //init layer
    syncFirst = [CALayer layer];

    //animatated content size init
    syncFirst.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
    syncFirst.position = CGPointMake(10, 10);
    syncFirst.contents = (id)[self convertToCGImageFromNasImage:image];

    /*HERE SETWANTSLAYER TO TRUE IN ORDER TO FIX*/
    [nsview setWantsLayer:YES];

    [nsview.layer addSublayer:syncFirst];
}

Before using this code

Please refer to the discussion in comment. There are some useful tips, Which I have overlooked while solving my problem. Credit goes to - Peter Hosey



来源:https://stackoverflow.com/questions/10836131/nsimage-rotation-in-nsview-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!