2014-12-03 86 views
0

此代碼運行良好,但分析時顯示潛在的內存泄漏。看起來imageCrop有潛在的內存泄漏。非常感謝您的幫助。存儲在'imageCrop'中的對象的潛在泄漏

這是我的代碼。

-(CATransformLayer *)transformLayerFromImage:(UIImage *)image Frame:(CGRect)frame Duration:(CGFloat)duration AnchorPiont:(CGPoint)anchorPoint StartAngle:(double)start EndAngle:(double)end 
{ 
    CATransformLayer *jointLayer = [CATransformLayer layer]; 
    jointLayer.anchorPoint = anchorPoint; 
    CALayer *imageLayer = [CALayer layer]; 
    CAGradientLayer *shadowLayer = [CAGradientLayer layer]; 
    double shadowAniOpacity; 

    if (anchorPoint.y == 0.5) { 
     CGFloat layerWidth; 
     if (anchorPoint.x == 0) //from left to right 
     { 
      layerWidth = image.size.width - frame.origin.x; 
      jointLayer.frame = CGRectMake(0, 0, layerWidth, frame.size.height); 
      if (frame.origin.x) { 
       jointLayer.position = CGPointMake(frame.size.width, frame.size.height/2); 
      } 
      else { 
       jointLayer.position = CGPointMake(0, frame.size.height/2); 
      } 
     } 
     else { //from right to left 
      layerWidth = frame.origin.x + frame.size.width; 
      jointLayer.frame = CGRectMake(0, 0, layerWidth, frame.size.height); 
      jointLayer.position = CGPointMake(layerWidth, frame.size.height/2); 
     } 

     //map image onto transform layer 
     imageLayer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); 
     imageLayer.anchorPoint = anchorPoint; 
     imageLayer.position = CGPointMake(layerWidth*anchorPoint.x, frame.size.height/2); 
     [jointLayer addSublayer:imageLayer]; 
     CGImageRef imageCrop = CGImageCreateWithImageInRect(image.CGImage, frame); 
     imageLayer.contents = (__bridge id)imageCrop; 
     imageLayer.backgroundColor = [UIColor clearColor].CGColor; 

     //add shadow 
     NSInteger index = frame.origin.x/frame.size.width; 
     shadowLayer.frame = imageLayer.bounds; 
     shadowLayer.backgroundColor = [UIColor darkGrayColor].CGColor; 
     shadowLayer.opacity = 0.0; 
     shadowLayer.colors = [NSArray arrayWithObjects:(id)[UIColor blackColor].CGColor, (id)[UIColor clearColor].CGColor, nil]; 
     if (index%2) { 
      shadowLayer.startPoint = CGPointMake(0, 0.5); 
      shadowLayer.endPoint = CGPointMake(1, 0.5); 
      shadowAniOpacity = (anchorPoint.x)?0.24:0.32; 
     } 
     else { 
      shadowLayer.startPoint = CGPointMake(1, 0.5); 
      shadowLayer.endPoint = CGPointMake(0, 0.5); 
      shadowAniOpacity = (anchorPoint.x)?0.32:0.24; 
     } 
    } 
    else{ 
     CGFloat layerHeight; 
     if (anchorPoint.y == 0) //from top 
     { 
      layerHeight = image.size.height - frame.origin.y; 
      jointLayer.frame = CGRectMake(0, 0, frame.size.width, layerHeight); 
      if (frame.origin.y) { 
       jointLayer.position = CGPointMake(frame.size.width/2, frame.size.height); 
      } 
      else { 
       jointLayer.position = CGPointMake(frame.size.width/2, 0); 
      } 
     } 
     else { //from bottom 
      layerHeight = frame.origin.y + frame.size.height; 
      jointLayer.frame = CGRectMake(0, 0, frame.size.width, layerHeight); 
      jointLayer.position = CGPointMake(frame.size.width/2, layerHeight); 
     } 

     //map image onto transform layer 
     imageLayer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); 
     imageLayer.anchorPoint = anchorPoint; 
     imageLayer.position = CGPointMake(frame.size.width/2, layerHeight*anchorPoint.y); 
     [jointLayer addSublayer:imageLayer]; 
     CGImageRef imageCrop = CGImageCreateWithImageInRect(image.CGImage, frame); 
     imageLayer.contents = (__bridge id)imageCrop; 
     imageLayer.backgroundColor = [UIColor clearColor].CGColor; 

     //add shadow 
     NSInteger index = frame.origin.y/frame.size.height; 
     shadowLayer.frame = imageLayer.bounds; 
     shadowLayer.backgroundColor = [UIColor darkGrayColor].CGColor; 
     shadowLayer.opacity = 0.0; 
     shadowLayer.colors = [NSArray arrayWithObjects:(id)[UIColor blackColor].CGColor, (id)[UIColor clearColor].CGColor, nil]; 
     if (index%2) { 
      shadowLayer.startPoint = CGPointMake(0.5, 0); 
      shadowLayer.endPoint = CGPointMake(0.5, 1); 
      shadowAniOpacity = (anchorPoint.x)?0.24:0.32; 
     } 
     else { 
      shadowLayer.startPoint = CGPointMake(0.5, 1); 
      shadowLayer.endPoint = CGPointMake(0.5, 0); 
      shadowAniOpacity = (anchorPoint.x)?0.32:0.24; 
     } 
    } 

    [imageLayer addSublayer:shadowLayer]; 

    //animate open/close animation 
    CABasicAnimation* animation = (anchorPoint.y == 0.5)?[CABasicAnimation animationWithKeyPath:@"transform.rotation.y"]:[CABasicAnimation animationWithKeyPath:@"transform.rotation.x"]; 
    [animation setDuration:duration]; 
    [animation setFromValue:[NSNumber numberWithDouble:start]]; 
    [animation setToValue:[NSNumber numberWithDouble:end]]; 
    [animation setRemovedOnCompletion:NO]; 
    [jointLayer addAnimation:animation forKey:@"jointAnimation"]; 

    //animate shadow opacity 
    animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
    [animation setDuration:duration]; 
    [animation setFromValue:[NSNumber numberWithDouble:(start)?shadowAniOpacity:0]]; 
    [animation setToValue:[NSNumber numberWithDouble:(start)?0:shadowAniOpacity]]; 
    [animation setRemovedOnCompletion:NO]; 
    [shadowLayer addAnimation:animation forKey:nil]; 

    return jointLayer; 
} 
+0

修復格式請。 – Andy 2014-12-03 11:41:55

回答

1
不管你用 CGImageCreateWithImageInRect創建

應該被釋放,因爲這是從非託管內存土地的功能。

  1. 使用CGImageRelease釋放參照非託管對象:

    CGImageRelease(imageCrop); 
    imageCrop = NULL; 
    

OR

  • 使用__bridge_transfer代替__bridge到將對象所有權通過CGImageRef轉移給ARC。

    imageLayer.contents = (__bridge_transfer id)imageCrop; 
    

    在這種情況下,ARC將有效地爲您做#1。

  • 好讀:

    1. 上有橋在Objective-C大後:https://stackoverflow.com/a/17256947/351305
    2. 及相關問題:Does ARC work with Core Graphics objects?
    +1

    非常感謝。 @Andy – 2014-12-03 13:39:39

    +0

    @asheeshVerma很樂於幫助:-) – Andy 2014-12-03 13:41:28

    +0

    @asheeshVerma請將我的答案標記爲正確,以便我可以獲得免費的糖果和更多的名氣。謝謝。 – Andy 2014-12-03 14:21:18