2014年3月2日 星期日

人非聖賢,孰能無過的常見bug清單

1. 記憶體 (memory)

1-1  memory  leak  
造成記憶體用量增加,最終導致App Crash

1-1-1  剪裁圖片 ( CGImageCreateWithImageInRect )

ex:

UIImage *originalImage = [UIImage imageNamed:@"test.png"];
CGRect cropRect = CGRectMake(0, 0, 320, 320);
CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], cropRect);
UIImage *cropImage = [UIImage imageWithCGImage:imageRef];
UIImageView *imageView = [[UIImageView alloc] initWithImage:cropImage];

問題:  忘記呼叫CGImageRelease

正確寫法:
UIImage *originalImage = [UIImage imageNamed:@"test.png"];
CGRect cropRect = CGRectMake(0, 0, 320, 320);
CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], cropRect);
UIImage *cropImage = [UIImage imageWithCGImage:imageRef];
UIImageView *imageView = [[UIImageView alloc] initWithImage:cropImage];
CGImageRelease(imageRef);


沒有留言:

張貼留言