1

I tried adapting Travis' example he gave me last week here (C4 saving part of an image) to save the screen as a C4Image. I thought it should work like this:

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

...even though the Log gives me

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

...nothing is displayed on screen... But yes, there was stuff on the canvas that should have been captured...

And again, I don't know whats wrong. Also other attempts using objectiveC don't work out (e.g., iOS Screenshot part of the screen)

Community
  • 1
  • 1
suMi
  • 1,536
  • 1
  • 17
  • 30
  • Your code works for me... Two qs, just to be clear: 1) Is this code running in your `C4WorkSpace.m`? 2) If not, are there things on the canvas in which this is running? – C4 - Travis Oct 29 '13 at 17:33
  • no it's not running in the main canvas and 2 yes there are things on canvas in the space where it's running. I'll try again tomorrow morning – suMi Oct 29 '13 at 18:18
  • Ok, so a subclassed C4CanvasController. I'll test this before tomorrow morning. – C4 - Travis Oct 29 '13 at 18:37

1 Answers1

0

What I found out is it needs a bit of a workaround. The code as shown above works fine in C4Workspace but when trying to save a subview into an image one

  1. needs to run the save function in the main view
  2. sends a notification to the main view to run this function
  3. the notification cannot be sent from the setup of that view.

So the code for me now looks like this C4Workspace.h

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

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

C4Workspace.m

import "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
Community
  • 1
  • 1
suMi
  • 1,536
  • 1
  • 17
  • 30