2016-04-14 13 views
0
- (void)drawingViewDidPan:(UIPanGestureRecognizer*)sender 
{ 
    CGPoint currentDraggingPosition = [sender locationInView:drawableView]; 

    if(sender.state == UIGestureRecognizerStateBegan){ 
    _prevDraggingPosition = currentDraggingPosition; 
    } 

    if(sender.state != UIGestureRecognizerStateEnded){ 
     [self drawLine:_prevDraggingPosition to:currentDraggingPosition]; 
    } 
    _prevDraggingPosition = currentDraggingPosition; 
} 

-(void)drawLine:(CGPoint)from to:(CGPoint)to 
{ 
    CGSize size = drawableView.frame.size; 
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    [drawableView.image drawAtPoint:CGPointZero]; 

    CGFloat strokeWidth = 4.0; 
    UIColor *strokeColor = [UIColor blackColor]; 

    CGContextSetLineWidth(context, strokeWidth); 
    CGContextSetStrokeColorWithColor(context, strokeColor.CGColor); 
    CGContextSetLineCap(context, kCGLineCapRound); 


    CGContextMoveToPoint(context, from.x, from.y); 
    CGContextAddLineToPoint(context, to.x, to.y); 
    CGContextStrokePath(context); 

    drawableView.image = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 
} 

私はUIImageView内に存在するImageを描画するのにこのコードを使用していますが、コードに何が間違っているのか警告してくれますか?UIImageで描画するとメモリ警告が発生する

イメージビューをイメージビューに追加した後、ユーザーがクリックすると、指定したイメージビューで描画可能イメージビューイメージを描画してイメージを作成します。

答えて

0

初めて私があなたの質問を読んだとき、私はあなたのコードにリークがあることを知っていました。私はiOSで7ヶ月の経験があります。問題はdrawableView.imageにあるはずです。取得しようとしているUIGraphicsGetImageFromCurrentImageContext()

autoreleasepoolでコードをラップしてみてください。

@autoreleasepool { 
// Wrap your code here... 
} 

最後に、メインスレッドでコードを実行していることを確認してください。

dispatch_async(dispatch_get_main_queue(), ^{ 
    // dispatch in main! 
}); 
関連する問題