2013-10-29 62 views
1

我試着調整Travis的例子,他上週給我這裏(C4 saving part of an image)將屏幕保存爲C4Image。 我認爲應該這樣工作:C4保存屏幕爲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

+0

您的代碼對我的作品...... 2個季度,僅僅是明確的:1)在你的'C4WorkSpace.m運行此代碼'? 2)如果沒有,在這個畫布上有什麼東西在運行? –

+0

不,它沒有在主畫布上運行,2是在它正在運行的空間的畫布上有東西。我明天早上再試一次 – suMi

+0

好的,所以C4CanvasController是一個子類。我會在明天早上測試這個。 –

回答

0

我發現它需要一些解決方法。如上所示的代碼工作正常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 

te stView.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