7

そのため、デバイスのフォト ストリームに保存するサブクラス化された UIView のスクリーンショットを撮っています。

問題:

問題はresizableImageWithCapInsets、引き伸ばされた背景を UIView に追加するために使用することですが、この背景が右側で途切れてしまい、その理由がわかりません。誰かが私を助けることができれば、それは非常に高く評価されます.

これは、アプリでの画像の外観 (左) と、フォト ストリームに保存した後の画像 (右) です。

次の方法で、引き伸ばされた背景を UIView に追加します。

[diagramBase addSubview:[self addTileBackgroundOfSize:diagramBase.frame 
andType:@"ipad_diagram_border.png"]];

このメソッドを呼び出すもの:

- (UIImageView *) addTileBackgroundOfSize:(CGRect)frame 
andType:(NSString *)type
{
frame.origin.x = 0.0f;
frame.origin.y = 0.0f;

UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:frame];
UIImage *image = [UIImage imageNamed:type];
UIEdgeInsets insets = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
UIImage *backgroundImage = [image resizableImageWithCapInsets:insets];
backgroundView.image = backgroundImage;

return backgroundView;
}

実際のプリントスクリーンはこのメソッドで行われます (RINDiagramView はサブクラス化された UIView の名前で、スクリーンショットを撮っています)。保存時に画像を回転させる必要があるため、回転はそこにありますが、その部分をコメントアウトしましたが、それは背景が奇妙な動作をするものではありません.

- (UIImage *) createSnapshotOfView:(RINDiagram *) view
{
CGRect rect = [view bounds];
rect.size.height = rect.size.height - 81.0f;
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *finalImage = [[UIImage alloc] initWithCGImage: capturedScreen.CGImage
                                            scale: 1.0
                                      orientation: UIImageOrientationLeft];
return finalImage;
}

私は Xcode 5.1 を使用しており、すべてがプログラムで行われます (ストーリーボードなどはありません)。ベース SDK は iOS 7.1 です。

4

4 に答える 4

3

iOS 7 以降を使用している場合は、drawViewHierarchyInRect:afterScreenUpdates:Apple が本当にパフォーマンスが高いと言っている新しい関連メソッドを使用できます。

iOS 6 をターゲットにしている場合でも、同じ問題が発生するかどうか試してみてください。

于 2014-03-31T01:04:57.890 に答える
2

正しいスケールを使用してみてください。

UIImage *finalImage = [[UIImage alloc] initWithCGImage: capturedScreen.CGImage
                                        scale: [[UIScreen mainScreen] scale]
                                  orientation: UIImageOrientationLeft];

別の UIViewContentMode を使用しますか?

UIViewContentModeScaleToFill -> check if you can see the edges
UIViewContentModeScaleAspectFit -> check if you can see the edges, even if position is incorrect
UIViewContentModeScaleAspectFill -> check for edge right side
于 2014-03-27T09:46:36.753 に答える
2
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"foo.png" atomically:YES];

Retina ディスプレイの場合、最初の行を次のように変更します。

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.window.bounds.size);

サイズを調整してください。助けが得られるかもしれません..

于 2014-04-02T09:18:04.763 に答える
2

右側のカット画像が得られた理由は、この行が原因です

UIImage *finalImage = [[UIImage alloc] initWithCGImage: capturedScreen.CGImage
                                            scale: 1.0
                                      orientation: UIImageOrientationLeft];

画像の向きを左にすると、コンテキストは左側が上部であると見なします。また、サイズには高さの値にマイナスがあるため、結果は右側に変わり、カットされます。

ローテーションについて、あなたのコードにいくつかのコードを追加しました。参考になれば幸いです。

- (UIImage *) createSnapshotOfView:(UIView *) view
{
    CGRect rect = [view bounds];
    rect.size.height = rect.size.height - 81.0f;
    UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();

    view.transform = CGAffineTransformMakeRotation(M_PI_2);
    [view.layer renderInContext:context];

    UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    UIImage *finalImage = [[UIImage alloc] initWithCGImage: capturedScreen.CGImage
                                                     scale: 1.0
                                               orientation: UIImageOrientationLeft];

    view.transform = CGAffineTransformMakeRotation(0);
    return finalImage;
}
于 2014-03-31T12:59:44.467 に答える