2013-05-16 82 views
0

我有應用程序在其中我正在用手指標誌它工作正常,它繪製線條,但我想要保存這些線條繪製的簽名作爲ImageView中的圖像來顯示屏幕但它不顯示我正在使用以下代碼。將繪製的視圖轉換爲UIImage並將其添加到UIImageView中iphone

-(void)onSignButtonClick 
{ 

    signatureView=[[UIView alloc] initWithFrame:CGRectMake(100,100,800,500)]; 

    signatureView.backgroundColor=[UIColor blackColor]; 

    [self.view addSubview:signatureView]; 

    UIButton*OkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [OkButton setFrame:CGRectMake(100,420,200,40)]; 
    [OkButton setTitle:@"OK" forState:UIControlStateNormal]; 
    [OkButton addTarget:self action:@selector(onOKButtonClick) forControlEvents:UIControlEventTouchUpInside]; 
    [signatureView addSubview:OkButton]; 

    drawScreen=[[MyLineDrawingView alloc]initWithFrame:CGRectMake(10,10,700,400)]; 
    [signatureView addSubview:drawScreen]; 
    [drawScreen release]; 
} 


- (UIImage *)captureView { 

    //hide controls if needed 
     CGRect rect = [signatureView bounds];//use your signature view's Rect means Frame; 
     UIGraphicsBeginImageContext(rect.size); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     [self.view.layer renderInContext:context]; 
     UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return img; 
} 

-(void)onOKButtonClick{ 


    signatureView.hidden=YES; 

    [self captureView]; 

     logoImageView.image=signatureImage; 
} 
+0

[self capt ureView];是返回UIImage ..你得到那個UIImage? – Yashesh

+0

我從網站得到了這段代碼,我認爲我們需要得到返回的圖像,請你幫我在 –

+1

使用logoImageView.image = [self captureView]; – Yashesh

回答

0

試試這個:發生

-(void)onOKButtonClick { 
    //first capture image from view 
    CGRect rect = [signatureView bounds]; //use your signature view's Rect means Frame; 

    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [signatureView.layer renderInContext:context]; //this line is also important for you 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    //take some part of image (crop image) 
    UIImage *imgFinal = [self cropImage:img rect:CGRectMake(100,100,300,300)]; 
    logoImageView.image = imgFinal; 

    signatureView.hidden=YES; 
} 

-(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect { 
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect); 
    UIImage *img = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return img; 
} 

問題,因爲你的captureView方法定義返回UIImage的,但你不能存儲它在方法調用....

+0

這工作正常,但它可能只捕捉視圖的意思如果視圖是500 * 500,但簽名是在300 * 300上完成,那麼它應該只能俘獲300 * 300,然後將它保存爲圖像 –

+0

或可能我使圖像視圖的大小更小,使它適合那個beacuaue它就像一個signaute,將被放置在文檔 –

+0

看到我編輯的答案爲您的問題的解決方案.... – DharaParekh

0

取代你

logoImageView.image=signatureImage; 

logoImageView.image = [self captureView]; 

編輯

然後更換

[self.view.layer renderInContext:context]; 

captureView方法與此:

[self.signatureView.layer renderInContext:context]; 
+0

我已經這樣做,但它保存了主視圖不是我在其中添加繪圖的代碼爲 –

+0

的signatureView檢查我的新編輯請 – Ushan87

0

在你的方法captureView使用此。這對我的作品

**UIGraphicsBeginImageContext(tempV.frame.size); 
[[tempV layer] renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();** 

也chnage你的函數類似這樣的

-(void)onOKButtonClick{ 
signatureView.hidden=YEs; 

    logoImageView.image=[self captureView]; 

} 
相關問題