【问题标题】:C4 save screen as C4ImageC4 将屏幕另存为 C4Image
【发布时间】:2013-10-29 09:06:53
【问题描述】:

我尝试改编 Travis 上周在此处 (C4 saving part of an image) 给我的示例,以将屏幕保存为 C4Image。 我认为它应该像这样工作:

CGFloat scale = 5.0;

//begin an image context
CGSize  rect=CGSizeMake(self.canvas.width, self.canvas.height);
UIGraphicsBeginImageContextWithOptions(rect, NO, scale);

//create a new context ref
CGContextRef c = UIGraphicsGetCurrentContext();

//render the original image into the context
[self.canvas renderInContext:c];

//grab a UIImage from the context
UIImage *newUIImage = UIGraphicsGetImageFromCurrentImageContext();

//end the image context
UIGraphicsEndImageContext();

//create a new C4Image
self.currentAlphabetImage = [C4Image imageWithUIImage:newUIImage];

C4Log(@"self.currentAlphabetImage:%@", self.currentAlphabetImage);
self.currentAlphabetImage.width=self.canvas.width/2;
self.currentAlphabetImage.center=self.canvas.center;
self.currentAlphabetImage.backgroundColor=navBarColor;
[self.canvas addImage:self.currentAlphabetImage];

...即使日志给了我

self.currentAlphabetImage:C4Image: 0x15840970; baseClass = UIControl; 
frame = (0 0; 320 568); layer = C4Layer: 0x15842590>>

...屏幕上什么都没有显示...但是是的,画布上有些东西应该被捕获...

再说一次,我不知道怎么了。其他使用 ObjectiveC 的尝试也没有成功(例如,iOS Screenshot part of the screen)

【问题讨论】:

  • 你的代码对我有用......两个问题,只是为了清楚:1)这段代码是否在你的C4WorkSpace.m中运行? 2)如果没有,画布上是否有运行它的东西?
  • 不,它没有在主画布中运行,2 是的,在它运行的空间中,画布上有东西。我明天早上再试一次
  • 好的,所以是 C4CanvasController 的子类。我会在明天早上之前测试一下。

标签: ios objective-c c4


【解决方案1】:

我发现它需要一些解决方法。上面显示的代码在 C4Workspace 中运行良好,但是在尝试将子视图保存到图像时

  1. 需要在主视图中运行保存功能
  2. 向主视图发送通知以运行此功能
  3. 无法通过该视图的设置发送通知。

所以我的代码现在看起来像这样 C4Workspace.h

#import "C4CanvasController.h"
#import "testView.h"

@interface C4WorkSpace : C4CanvasController{
    testView *test;
}
@end

C4Workspace.m

导入“C4Workspace.h”

@implementation C4WorkSpace

-(void)setup {
    test= [testView new];
    test.canvas.frame=CGRectMake(0, 0, self.canvas.width, self.canvas.height);
    [test setup];
    [self.canvas addSubview:test.canvas];

    [self listenFor:@"save" andRunMethod:@"save"];

}
-(void)save{
    CGFloat scale = 5.0;
    
    //begin an image context
    CGSize  rect=CGSizeMake(self.canvas.width, self.canvas.height);
    UIGraphicsBeginImageContextWithOptions(rect, NO, scale);
    
    //create a new context ref
    CGContextRef c = UIGraphicsGetCurrentContext();
    
    //render the original image into the context
    [test.canvas renderInContext:c];
    
    //grab a UIImage from the context
    UIImage *newUIImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //end the image context
    UIGraphicsEndImageContext();
    
    //create a new C4Image
    test.currentAlphabetImage = [C4Image imageWithUIImage:newUIImage];
    
    C4Log(@"self.currentAlphabetImage:%@", test.currentAlphabetImage);
    test.currentAlphabetImage.width=test.canvas.width/2;
    test.currentAlphabetImage.center=test.canvas.center;
    [test.canvas addImage:test.currentAlphabetImage];
}
@end

testView.h

#import "C4CanvasController.h"

@interface testView : C4CanvasController{
    C4Shape *shape;
}
@property (readwrite, strong) C4Image *currentAlphabetImage;
@end

testView.m

#import "testView.h"

@implementation testView

-(void)setup{

    shape=[C4Shape rect:CGRectMake(0, 0, self.canvas.width, self.canvas.height)];
    shape.lineWidth=3;
    shape.fillColor=[UIColor colorWithRed:0.5 green:1 blue:0.2 alpha:1];
    [self.canvas addShape:shape];
    [self listenFor:@"touchesBegan" andRunMethod:@"saveFunc"];
    
}
-(void)saveFunc{
    [self postNotification:@"save"];
    C4Log(@"saving");
}

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-05
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 2022-12-07
    相关资源
    最近更新 更多