2011-10-16 5 views
3

UITextViewのコンテンツのビットマップを取得しようとしています。私はと画面上に現在あるUITextViewのコンテンツのビットマップを取得することができるよ:UITextViewのコンテンツ全体のビットマップを作成します。

UIGraphicsBeginImageContext(myTextView.bounds.size); 
[myTextView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 
myImageView.image=resultingImage; 

UIGraphicsEndImageContext(); 

をしかし、私はすべてのコンテンツのビットマップ、画面上に表示されていないだけで、コンテンツをしたいです。これは可能ですか?私はUITextViewのテキストだけを望んでいないことに注意してください。私はコンテンツ自体のビットマップが必要です。私は周りを検索し、どのようにAndroidで、getDrawingCacheでこれを行うが、目的cのためのものを見つけることができませんでした。

+2

これを見てみましょう:http://stackoverflow.com/questions/3539717/何らかの理由で、より正確なコピーが必要な場合は、その後、1次の操作を行うことができスクリーンショットを撮るスクリーンショットを含む3539944#3539944 – Mat

+0

正確に何が必要なのですか@Matありがとうございました! –

+0

あなたは大歓迎です...(長いテーブルビューのような)膨大なuiviewをレンダリングする場合、このメソッドは実行に時間がかかります。 – Mat

答えて

0

これを行う1つの方法は、UITextViewのコピーを作成し、コンテンツのサイズ(コンテンツサイズがフレームサイズより大きい場合)にコピーのサイズを変更することです。

はスウィフトでは、次のようになります。これは、コンテンツのすべての画像を取得することができます

func imageFromTextView(textView: UITextView) -> UIImage { 

    // Make a copy of the textView first so that it can be resized 
    // without affecting the original. 
    let textViewCopy = UITextView(frame: textView.frame) 
    textViewCopy.attributedText = textView.attributedText 

    // resize if the contentView is larger than the frame 
    if textViewCopy.contentSize.height > textViewCopy.frame.height { 
     textViewCopy.frame = CGRect(origin: CGPointZero, size: textViewCopy.contentSize) 
    } 

    // draw the text view to an image 
    UIGraphicsBeginImageContextWithOptions(textViewCopy.bounds.size, false, UIScreen.mainScreen().scale) 
    textViewCopy.drawViewHierarchyInRect(textViewCopy.bounds, afterScreenUpdates: true) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return image 
} 

、一部でもそのスクロールせず見えないインチ

ノート

  • マイもう少し一般的な答えはhereです。
  • textViewCopyは、フレームと属性付きテキストのコピーです。それは完全なイメージを得るのに十分なはずです。 (explanation

    let tempArchive = NSKeyedArchiver.archivedDataWithRootObject(textView) 
    let textViewCopy = NSKeyedUnarchiver.unarchiveObjectWithData(tempArchive) as! UITextView 
    
関連する問題