何时用self引用会加1,[UIImage imageNamed]使用

代码粘的多了点,看着清楚一点
SetupAccountViewController.h文件,声明m_comBoxView,线程不安全,retain(self调用引用+1)
#import <UIKit/UIKit.h>
#import "ComBoxView.h"

@interface SetupAccountViewController : UIViewController
{
    ComBoxView *m_comBoxView;
}
@property (retain,nonatomic) ComBoxView *m_comBoxView;
@end


接下来看看m文件
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray* array=[NSMutableArray arrayWithObjects:@"btn_img_listBox", @"btn_img_bolg_background", @"btn_img_listBox", @"btn_img_bolg_background", @"btn_img_listBox", @"btn_img_bolg_background", nil];

    //114 view宽,150view tableview+btn高度
    CGRect frame = CGRectMake(180, 46, 114, 150);


    //注意为什么新alloc一个ComBoxView呢,直接调用self.m_comBoxView是没问题的,重新alloc是为了今后有跟子线程交互时更安全,而且保证self.m_comBoxView引用级数为1,不会发生+2+3的情况
    ComBoxView *comBoxView = [[ComBoxView alloc] initWithFrame:frame];
    comBoxView.m_downListDataSource = array;
    self.m_comBoxView = comBoxView;
    //tab默认背景黑
    m_comBoxView.backgroundColor = [UIColor clearColor];
    [m_comBoxView setImgLeftMark:[array objectAtIndex:0]];
    [comBoxView release];
    
    
    self.title = kViewtitle;
    [self.view addSubview:m_comBoxView];
}

self.m_comBoxView = comBoxView;
这一句比较重要,就是说只有self调用赋值,这个时候引用技术才会+1
m_comBoxView.backgroundColor = [UIColor clearColor];改成
self.m_comBoxView.backgroundColor = [UIColor clearColor];作用相同,也不会影响引用计数,因为这里是调用属性。


下面这个还没有遇到过,但是有朋友说,所以记录一下!大家都注意
myImage = [UIImage imageNamed:@"icon.png"];
这种方法在application bundle的顶层文件夹寻找由供应的名字的图象 。 如果找到图片,装载到iPhone系统缓存图象。那意味图片是(理论上)放在内存里作为cache的。
所以使用小图或者少量图是可以的,大量图则该选用
NSString *path = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"];
myImage = [UIImage imageWithContentsOfFile:path];

不过这种方法要注意了,如果有高清图,也就是@2x的,就不能自动获取到了,自己把握吧,做ui可以用imageNamed,要处理的大图就别这样加载了

猜你喜欢

转载自zcw-java.iteye.com/blog/1842983