簡體   English   中英

在iOS中更改UIView的bounds屬性,然后渲染圖像

[英]changing the bounds property of UIView in iOS, then rendering image

在我的應用中:

  • 我有一個clipsToBound = YES的視圖(UIView * myView);

  • 我有一個按鈕可以更改bounds屬性的來源:

 CGRect newRect = myView.bounds; newRect.origin.x += 100; myView.bounds = newRect; myView.layer.frame = newRect; 
  • 然后我從視圖中獲得圖像:
 UIGraphicsBeginImageContext(myView.bounds.size); [myView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage_after = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(viewImage_after, nil, nil, nil); 

它會產生意想不到的圖像。 我想要一張在iPhone屏幕上看到的圖像。

此處的代碼鏈接: http : //www.mediafire.com/?ufr1q8lbd434wu1

請幫我!

您不想在上下文中渲染圖像本身-總是以相同的方式渲染(您沒有更改圖像,只是在移動視圖的遠處)。

您想像這樣渲染圖像的父視圖:

UIGraphicsBeginImageContext(myView.superview.bounds.size);
[myView.superview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage_after = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage_after, nil, nil, nil);

編輯

但是,您可能不希望在超級視圖中呈現所有內容:)

您的意見可能看起來像

MainView
   ImageView (myView)
   UIButton (ok button)
   UIButton (cancel button)

在這里,渲染圖像的超級視圖將渲染MainView-包括按鈕!

您需要像這樣在層級中添加另一個視圖:

MainView
  UIView (enpty uiview)
    ImageView (myView)
  UIButton (ok button)
  UIButton (cancel button)

現在,當您渲染圖像的超級視圖時,僅在其中包含圖像-按鈕不會被渲染:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM