0

マスクされた画像がたくさんあるアプリがあります。パフォーマンスのために、これらのマスクされたイメージをディスク上に生成し、アプリに組み込む必要がありました (既に完了しているため、オンザフライでマスクされずにオンデマンドでアップロードされます)。

私はこの種のコーディングを使用しています

NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/Test_%d.png",i]];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/Test_%d.jpg",i]];

// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation([self maskImageWithStroke:image withMask:maskImage], 1.0) writeToFile:jpgPath atomically:YES];

// Write image to PNG
[UIImagePNGRepresentation([self maskImageWithStroke:image withMask:maskImage]) writeToFile:pngPath atomically:YES];

私の中間画像では完全に機能しますが、最終画像では機能しません。

私が使用する複数のマスクとブランのプロセスは次のとおりです。

  1. 画像を取得し、それをマスクして maskedImage を取得します

  2. リスト アイテムのマスクを取得し、サイズを少し大きくします。

  3. maskedImage と bigmask をブレンドして、maskedStrokedImage を biggrMask でストロークします (これは、不規則なマスク イメージに不規則なストロークを追加する唯一の方法です)。

  4. 最終的な結果を得るために、 maskedStrokedImage を bigMask でマスクします。

問題は次のとおりです。ステップ 1 で取得した画像を保存しても問題ありません。必要なものが正確に含まれた JPG と PNG があります。

私の目標は、ステップ 4 の結果をディスクに保存することですが、結果はストロークの一部を示す imahe であり、残りは白です ...

ステップ 4 をディスクに保存できない理由はありますか?

4

1 に答える 1

3

ここに解決策がありますhttps://devforums.apple.com/thread/67565?tstart=0

透明な背景に黒い形状のマスクを使用し、次の方法を使用します。マスクされた画像はディスクに完全に保存され、透明性を維持したまま画面にインポートできます...グルーヴィー!!!

-(UIImage*)maskImage:(UIImage *)givenImage withClippingMask:(UIImage *)maskImage 
{
 UIGraphicsBeginImageContext(givenImage.size);
 CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
 CGContextTranslateCTM(currentContext, 0, givenImage.size.height);
 CGContextScaleCTM(currentContext, 1.0, -1.0);
 CGRect rectSize = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height);
 CGContextClipToMask(currentContext,rectSize, maskImage.CGImage);
 CGContextDrawImage(currentContext, rectSize, givenImage.CGImage); 
 UIImage *imgMasked = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return imgMasked; 
}
于 2010-10-19T07:36:23.377 に答える