0

UIImageView を含む UIView があり、UIImageView に回転変換を適用し、CGContext で同じ回転変換を使用してその画像を描画したい (そして、将来的には、変換やスケールなどの変換がさらに使用される予定です)、次のコードがあります。 CGContext で変換と描画を行います。

UIImage *image=[UIImage imageNamed:@"cena-1.png"];
CGRect imageFrame=CGRectMake(50, 50, image.size.width, image.size.height);
UIImageView *imageView=[[UIImageView alloc] initWithFrame:imageFrame];
imageView.image=image;
[self.view addSubview:imageView];

CGAffineTransform imageTransform=CGAffineTransformMakeRotation(degreesToRadians(10));
imageView.transform=imageTransform;
NSLog(@"self.imgOverlay.image.scale: %f", imageView.image.scale);

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);

CGContextConcatCTM(context, imageView.transform);

CGRect modifiedRect=CGRectApplyAffineTransform(imageFrame, imageTransform);
[imageView.image drawInRect:modifiedRect];

UIGraphicsPopContext();
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSString *jpgPath=[NSTemporaryDirectory() stringByAppendingPathComponent:@"test.jpg"];
NSData *imageData=UIImageJPEGRepresentation(outputImage, 1.0);
[imageData writeToFile:jpgPath atomically:YES];

NSLog(@"putputImage path: %@", jpgPath);

.

問題は、出力画像が UIKit の UIVIew に表示されているものと同じに見えないことです。歪んでいて、位置も正確ではありません。スクリーンショットは次のとおりです。

ここに画像の説明を入力

私が間違っていることを教えてください。助けてくれてありがとう:)

ps 別の質問 ( https://stackoverflow.com/questions/17873202/uiimage-drawn-in-cgcontext-with-cgaffinetransform-unexpected-results )も投稿しましたが、完全なシナリオを説明していますが、この質問への回答で十分だと思います.

更新[imageView.image drawAtPoint:modifiedRect.origin];スキューの問題(http://screencast.com/t/v2oKkmkd )を修正 しようとしましたが、位置の問題は残ります:(

更新 2 コードを少し変更したテスト プロジェクトを次に示します: https://github.com/abduliam/TestTransform

4

1 に答える 1

1

四角形を回転させるのではなく、描画コンテキスト全体を回転させてから、その四角形に描画します。

たとえば、これは -15 度回転した画像を描画します。

    CGRect clipped = CGRectInset(screenRect, 100, 100);
    CGContextRotateCTM(ctx,  -15 * M_PI / 180.0);
    [imageView.image drawInRect: clipped];
于 2014-12-31T13:39:53.993 に答える