Posted By

oppey on 05/23/11


Tagged


Versions (?)

UIImageView メモリリークとなることがある。


 / Published in: Objective C
 

  1. UIImageViewで画像を使うとき、下記のようにimageNamedを使って画像ファイルを読み込むとキャッシュされるので、メモリリークとなることがある。
  2. UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"Photo.png"]];
  3. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  4.  
  5.  
  6. 代替策として、dataWithContentsOfFileを使うとキャッシュを使わずに画像を表示することができる。
  7. NSString *fileLocation = [[NSBundle mainBundle] pathForResource:@"Photo" ofType:@"png"];
  8. NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
  9.  
  10. UIImage *image = [UIImage imageWithData:imageData];
  11. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

Report this snippet  

You need to login to post a comment.