方法1:
UIImage *image = [UIImage imageNamed:@"huahuo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(10, 50, self.frame.size.width - 20, 100);
[self addSubview:imageView];
[imageView release];
方法2
NSString *path = [NSBundle mainBundle] pathForResource:@"huahuo" ofType:@".png"];
UIImage *image = [UIImage alloc] initWithContentsOfFile:path] ;
UIImageView *imageView = [[UIImaneView alloc] initWithImage:image];
imageView.frame = CGrectMake(10, 50, self.frame, size.width - 20, 100);
[self addSubview:imageView];
[imageView release];
兩種方法的不同點:
方法一:用imageName加載圖片的時候,實質上系統會把圖片緩存到內存中去,如果圖片較大或者圖片較多的情況下用這張方法會消耗很大的內存,而且對於圖片的內存釋放也是一件非常複雜的事情,而對於同一張圖片,只會緩存一次,對於重複利用非常有優勢
方法二:initWithContentOfFile這個方法就是通過查找圖片在工程中的位置,然後去加載這張圖片,圖片數據不會被緩存,對於圖片較大,而且使用情況較少的情況下,使用此方法可以節省內存消耗