本文介绍了NSView中的NSImage旋转不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述











图片:











头文件






  #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






执行文件,只有相关方法






   - (void)awakeFromNib {

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

//动画内容大小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];
}

/ *转换CGImage中的NSImage * /

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

/ *当按钮点击动画图像时* /

- (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];
}






问题:






基本上,我想点击一个按钮后旋转此图片。我尝试的代码不是100%的实现,但从几个不同的地方,包括苹果文档。现在,很明显,我做一些天真的错误,但逻辑上我的代码似乎对我正确。

解决方案

对于任何未来访问者,代码工作完美,你可以使用它,如果你想除了确保之前添加图层在nsview我们必须设置nsview想层。



所以,更改将... ...






   - (void)awakeFromNib {

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

//动画内容大小init
syncFirst.bounds = CGRectMake(0,0,image.size.width,image.size.height);
syncFirst.position = CGPointMake(10,10);
syncFirst.contents =(id)[self convertToCGImageFromNasImage:image];

/ *这里的SETWANTSLAYER为正确的顺序到FIX * /
[nsview setWantsLayer:YES];

[nsview.layer addSublayer:syncFirst];
}






/ h2>

请参阅评论中的讨论。有一些有用的提示,我在解决我的问题时忽略了。信用 -


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.

解决方案

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

这篇关于NSView中的NSImage旋转不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 23:36