2012-02-27 5 views
0

私はドラッグ&ピンチと回転が可能な変形可能なUIImageViews(写真に飾る)を用意しています。編集後、私はアプリに最終的な画像を生成させたい。どのように変換プロパティを考慮してUIImageViewsを描画することができます。iOSの変形を使用してUIImageを描画します。

最初の解決策はrenderInContextですが、320x480の画像しか生成できません。任意の解像度で画像を生成できますか?次のように

その後、私はコードを使用します。

UIGraphicsBeginImageContext(CGSizeMake(640.0f, 896.0f)); 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fittingBackground" ofType:@"jpg"]]; 
[backgroundImage drawInRect:CGRectMake(0.0f, 0.0f, 640.0f, 896.0f)]; 

CGContextSaveGState(currentContext); 
CGContextConcatCTM(currentContext, self.photoImageView.transform); 
CGRect photoFrame = self.photoImageView.frame; 
[self.photoImageView.image drawInRect:CGRectMake(photoFrame.origin.x * 2, photoFrame.origin.y * 2, photoFrame.size.width * 2, photoFrame.size.height * 2)]; 
CGContextRestoreGState(currentContext); 

CGContextSaveGState(currentContext); 
CGContextConcatCTM(currentContext, self.productImageView.transform); 
CGRect productFrame = self.productImageView.frame; 
[self.productImageView.image drawInRect:CGRectMake(productFrame.origin.x * 2, productFrame.origin.y * 2, productFrame.size.width * 2, productFrame.size.height * 2)]; 
CGContextRestoreGState(currentContext); 

UIImage *resultImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(currentContext)]; 

UIGraphicsEndImageContext(); 

しかし、結果は奇妙なことのようです。そして、私は枠、境界、中心を記録しました。変換を適用した後にRectdrawInRectで使用する必要がありますか?

+0

私のアプリではrenderInContextを使用していますが、これは320x480解像度に限定されているわけではありません。 – tarmes

+0

私のビューが320x480なら結果イメージは320x480です。 640×960に拡大できますか?私はバックグラウンドでビューのより大きなコピーを保持する必要がありますか? –

答えて

0

は、最も重要な行は UIGraphicsBeginImageContext(sizeBack)です

-(void)drawBackgroundInLayer:(CALayer*)destinationLayer { 

    CGFloat scaledFactor = [[UIScreen mainScreen] scale]; 
    CGRect layerFrame = destinationLayer.frame; 
    CGRect scaledRect = CGRectMake(layerFrame.origin.x, 
            layerFrame.origin.y, 
            (layerFrame.size.width)*scaledFactor, 
            (layerFrame.size.height)*scaledFactor); 

    // Get the size and do some drawings 
    CGSize sizeBack = CGSizeMake(layerFrame.size.width*scaledFactor, layerFrame.size.height*scaledFactor); 
    UIGraphicsBeginImageContext(sizeBack); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetRGBFillColor(context, 0., 0., 0., 1.); 
    drawBorder(context, scaledRect); 

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    if (destinationLayer) { 
     [destinationLayer setContents:(id)img.CGImage]; 
    } 
} 

、これを試してみてください。UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

+0

あなたのコードには変換がありません。 –

+0

このコードは私の個人的なプロジェクトのものですが、CoreGraphicsでカスタムサイズのUIImageを生成できることをお見せしようとしていました。このUIGraphicsBeginImageContext(sizeBack)を使用してみてください。 UIImage * img = UIGraphicsGetImageFromCurrentImageContext();あなたのコードで。 – Ganzolo

+0

申し訳ありませんが、おそらく私の質問が十分に明確ではありません。私は変換を適用した後に、変換された画像を描画するためにrect(サイズと原点)を使用する必要があります。 –

関連する問題