1

私は、Travis が先週私にくれた例 ( 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 スクリーンショット部分)

4

1 に答える 1

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

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
于 2013-10-30T07:59:21.187 に答える