0

再スケーリングされたコピーを取得するために、このUIImage拡張機能を作成しました。

-(UIImage*)scaleByRatio:(float) scaleRatio
{ 
    CGSize scaledSize = CGSizeMake(self.size.width * scaleRatio, self.size.height * scaleRatio);

    //The output context.   
    UIGraphicsBeginImageContext(scaledSize);
    CGContextRef context = UIGraphicsGetCurrentContext();

//Percent (101%)    
#define SCALE_OVER_A_BIT 1.01

    //Scale.
    CGContextScaleCTM(context, scaleRatio * SCALE_OVER_A_BIT, scaleRatio * SCALE_OVER_A_BIT);
    [self drawAtPoint:CGPointZero];

    //End?
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

ほとんどの場合は機能しますが、最近のプロジェクトでは元の画像を出力します(UIImageJPEGRepresentationimageWithDataを使用してスケーリングした直後にディスクに保存します:) 。

バックグラウンドスレッド内でメソッドを呼び出します。これが問題でしょうか?どうすればこれをスレッドセーフに書き直すことができますか(問題がスレッドによって引き起こされていると仮定します)。

4

1 に答える 1

2
-(UIImage*)scaleByRatio:(float) scaleRatio
{ 
   CGSize scaledSize = CGSizeMake(self.size.width * scaleRatio, self.size.height * scaleRatio);
   CGColorSpaceRef colorSpace;
   int   bitmapBytesPerRow;
   bitmapBytesPerRow   = (size.width * 4);

   //The output context.   
   UIGraphicsBeginImageContext(scaledSize);
   CGContextRef context = context = CGBitmapContextCreate (NULL,
                                 scaledSize .width,
                                 scaledSize .height,
                                 8,      // bits per component
                                 bitmapBytesPerRow,
                                 colorSpace,
                                 kCGImageAlphaPremultipliedLast);

 //Percent (101%)    
 #define SCALE_OVER_A_BIT 1.01

    //Scale.
   CGContextScaleCTM(context, scaleRatio * SCALE_OVER_A_BIT, scaleRatio * SCALE_OVER_A_BIT);
[self drawAtPoint:CGPointZero];

   //End?
   UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return scaledImage;
 }

つまり、UIGraphicsGetCurrentContext()をCGContextRef使用せずに作成する必要があります。CGBitmapContextCreateUIGraphicsGetCurrentContext();のため 安全ではありません。

願わくば、これはあなたを助けるでしょう...お楽しみください

于 2012-04-25T16:53:03.050 に答える