私はARCプロジェクトを持っており、垂直線グラデーションを描画しようとしています。以下のコードはシミュレータで動作しますが、デバイス上でテストするとメモリ/ EXC_BAD_ACCESS
エラーがスローされます。 (参考のために提供される)コードのARCを使用してグラデーションを描画するときのCFArrayRefとNSArrayの問題
NSArray *colorArray = [NSArray arrayWithObjects:(__bridge id)topColor, (__bridge id)bottomColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colorArray, colorLocations);
これらの2行は、次のコードから取られる:アプリケーションがコードの次の2行とにクラッシュ
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[self createGradientForContext:context andView:self.captionView];
[self createGradientForContext:context andView:self.linksView];
[self createGradientForContext:context andView:self.commentView];
}
- (void)createGradientForContext:(CGContextRef)context andView:(UIView *)view
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat colorLocations[] = { 0.0f, 1.0f };
CGColorRef topColor = [[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] CGColor];
CGColorRef bottomColor = [[UIColor colorWithRed:48.0f/255.0f green:48.0f/255.0f blue:48.0f/255.0f alpha:1.0f] CGColor];
NSArray *colorArray = [NSArray arrayWithObjects:(__bridge id)topColor, (__bridge id)bottomColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge_retained CFArrayRef) colorArray, colorLocations);
CGRect frame = view.frame;
CGPoint startPoint = CGPointMake(CGRectGetMidX(frame), CGRectGetMinY(frame));
CGPoint endPoint = CGPointMake(CGRectGetMidX(frame), CGRectGetMaxY(frame));
CGContextSaveGState(context);
CGContextAddRect(context, frame);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
おかげを前もって任意およびすべてのガイダンス。
アークが好きではないですか?プログラマがメモリを制御することをやめて...私は、Appleがガベージコレクションの教訓を学んだと思った。私はそれを使用しないことをお勧めします... –
私はARCを使用することをお勧めします、本当に魅力的な理由にする必要があります。 – zaph
他のすべてが 'CFType'のときにオブジェクトを使用しているのはなぜですか? 'CFArray'を使うだけで、ブリッジを心配する必要はありません。 –