2

現在のビューでCGPath描いている矢印のような形をしています。CGContext矢印のミニチュア バージョン (サムネイル) を生成して、選択したすべての矢印を表示するように追加したいと思いImageますUITableView

コンテキスト全体の画像をダウンスケールすることに成功しているため、矢印が本来よりも小さくなっています。理想的には、完全なコンテキストの画像を矢印の境界までトリミングしたいと思います。しかし、私はまだ成功していませんでした。手がかりはありますか?助けてくれてありがとう!

これは、矢印を含む完全なビューの写真と、私が生成しているサムネイルの別の写真です。 ここに画像の説明を入力 ここに画像の説明を入力

理想的には、上記のサムネイルは、完全なコンテキストではなく、矢印のみが含まれるように切り取られます。

私が使用するコードは次のとおりです。

- (UIImage*) imageForObject:(id<GraphicalObject>) object 
                     inRect:(CGRect)rect {

    UIImage *image = [UIImage new];
    CGRect objectBounds = [object objectBounds];
    UIGraphicsBeginImageContext(self.view.frame.size);//objectBounds.size);
    CGContextRef context =UIGraphicsGetCurrentContext();
    [object drawInContext:context];
    //doesn't work 
    CGContextClipToRect(context, objectBounds);

    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
4

1 に答える 1

0

CGRect呼び出された には、と のobjectBounds2 つのコンポーネントがoriginありsizeます。オブジェクトをサムネイルとして正しく描画するには、コードで画像を拡大縮小し (適切なサイズにする)、画像を変換する (原点を に移動する{0,0}) 必要があります。したがって、コードは次のようになります

- (UIImage *)getThumbnailOfSize:(CGSize)size forObject:(UIBezierPath *)object
{
    // to maintain the aspect ratio, we need to compute the scale
    // factors for x and y, and then use the smaller of the two
    CGFloat xscale = size.width / object.bounds.size.width;
    CGFloat yscale = size.height / object.bounds.size.height;
    CGFloat scale = (xscale < yscale) ? xscale : yscale;

    // start a graphics context with the thumbnail size
    UIGraphicsBeginImageContext( size );
    CGContextRef context = UIGraphicsGetCurrentContext();

    // here's where we scale and translate to make the image fit
    CGContextScaleCTM( context, scale, scale );
    CGContextTranslateCTM( context, -object.bounds.origin.x, -object.bounds.origin.y );

    // draw the object and get the resulting image
    [object stroke];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
于 2015-08-06T04:10:47.660 に答える