2012-01-02 17 views
16

私のコードでわかるように、私はスクリーンショットを撮ってフォトアルバムに保存します。 特定のエリアのスクリーンショットをプログラムで取得する

//for retina displays 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale); 
} else { 
    UIGraphicsBeginImageContext(self.view.bounds.size); 
} 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 

は、初めに私の代わりに self.view.bounds.sizewebview.sizeを使用してビューが 0/0に位置していたので、それが正常に働いていました。しかし、今私はWebViewを中心にしていますが、画像は指定サイズの 0/0から始まります。

スクリーンショットが指定サイズの別のlocation(たとえば300/150)で始まるように設定するにはどうすればよいですか?

UIWebViewの写真を撮る別の方法はありますか?

+0

これはデスクトップOSXまたはiphoneだけで動作しますか? – Noitidart

答えて

2

私は私の問題を解決しますが、質問に答えていませんでした:

私はUIViewのサブクラスを作成し、そこに私のUIWebViewを移動するので、サブクラスに対して0/0に配置されていることを示します。次に、WebViewのサイズを使用してください:

UIGraphicsBeginImageContext(webview.frame.size); 

と上記の他のコード。

これで、サブクラスを必要なすべての場所に移動できます。

ノート:日付/時刻ラベルのようなラベルがある場合は、それらも移動する必要があります。そうしないと、画像上に表示されません。

のでUIViewのサブクラスを作成し、そこに絵に を見られたくあらゆるIBOutletを移動します。

質問はまだ開いています: 画面の特定の領域を撮影することは可能ですか?

親切にしてください。 $ Hする@ RKYあなただけの画面の一部をつかむするために、2つの変更を行う必要があり

0
- (void)drawRect:(CGRect)rect { 
    UIGraphicsBeginImageContext(self.bounds.size);  
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
} 

あなたのビューにこれを呼び出すことができます]

+0

Webビューを作成するときにdrawRectメソッドが呼び出されます。最初は灰色の画像が表示されます。 私はこれから得たアイデアは、コードをドラッグして私のWebビューが0/0にあるカスタムUIViewクラスにドロップすることです。それは動作しますが、ラベルは「ルートビュー」の一部であるため、私はもうラベルを見ませんでした。 – Sharky

23

  1. は小さい画像を作成します - あなたがキャプチャしたい矩形のサイズを使用します。
  2. レンダリングするコンテキストの原点を、キャプチャしたいrectの負のx/yに移動します。

その後、レイヤをコンテキスト内にレンダリングするだけで、探しているものを得ることができます。このようなものがあります:

CGRect grabRect = CGRectMake(40,40,300,200); 

//for retina displays 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
    UIGraphicsBeginImageContextWithOptions(grabRect.size, NO, [UIScreen mainScreen].scale); 
} else { 
    UIGraphicsBeginImageContext(grabRect.size); 
} 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y); 
[self.view.layer renderInContext:ctx]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
+0

これは私のアプリをクラッシュさせます。どんな考え?私はQuartzCoreフレームワークを追加してこのコードを関数に入れましたが、それでもやりません。 –

+0

@rebellion - 私はこれを動作させました。写真のアルバムではないにもかかわらず。 UIViewカテゴリメソッドとして私の適応を下に参照してください。 – idStar

+0

ありがとう、これは私にとって素晴らしい作品です。ちょっとした問題は、画像の背景色が白です。私はそれを透明にしたいのです。どうやってやるの。助けてください – iMemon

10

私は上記の@ escraffordの答えを取ってテストしたところ、うまく動作しました。私はそれを 'grabRect'がparamaterizedできるようにUIViewでカテゴリメソッドにするというコンテキストでテストしました。UIImageViewを返すUIViewカテゴリとしてのこのコードは次のとおりです。

/** 
Takes a screenshot of a UIView at a specific point and size, as denoted by 
the provided croppingRect parameter. Returns a UIImageView of this cropped 
region. 

CREDIT: This is based on @escrafford's answer at http://stackoverflow.com/a/15304222/535054 
*/ 
- (UIImageView *)rp_screenshotImageViewWithCroppingRect:(CGRect)croppingRect { 
    // For dealing with Retina displays as well as non-Retina, we need to check 
    // the scale factor, if it is available. Note that we use the size of teh cropping Rect 
    // passed in, and not the size of the view we are taking a screenshot of. 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES, [UIScreen mainScreen].scale); 
    } else { 
     UIGraphicsBeginImageContext(croppingRect.size); 
    } 

    // Create a graphics context and translate it the view we want to crop so 
    // that even in grabbing (0,0), that origin point now represents the actual 
    // cropping origin desired: 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y); 
    [self.layer renderInContext:ctx]; 

    // Retrieve a UIImage from the current image context: 
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // Return the image in a UIImageView: 
    return [[UIImageView alloc] initWithImage:snapshotImage]; 
} 
+0

このコードはもちろん、ARCを前提としており、iOS6.1のように最新のものです。 – idStar

+1

素晴らしいソリューション!ありがとう! – MaKo

関連する問題