iOS中自动释放问题?(2)
来源:未知 责任编辑:责任编辑 发表时间:2014-01-26 21:59 点击:次
//path成员变量,文件路径-自动释放对象
path=[docstr stringByAppendingPathComponent:@"1.png"];
//调用方法,path做参数
[self saveData:path];
//分割线
-(void)saveData:(NSString *)filePath
{
//data读取内容
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"IMG_1340" ofType:@"PNG"];
NSData *data= [NSData dataWithContentsOfFile:imagePath];
//用path去保存data
[data writeToFile:filePath atomically:NO];
} 例子2,这么写,也没问题,文件能保存。或者[self saveData:path]; 改成[self saveData]; saveData方法不传入参数,用成员变量path代替filePath的作用,这么写,也没问题。[html] view plaincopyprint? //例子3
NSArray*patharray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*docstr=[patharray objectAtIndex:0];
//path成员变量,文件路径-自动释放对象
path=[docstr stringByAppendingPathComponent:@"1.png"];
//创建按钮
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0.0, 0.0, 100, 100)];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
-(void)saveData
{
//data读取内容
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"IMG_1340" ofType:@"PNG"];
NSData *data= [NSData dataWithContentsOfFile:imagePath];
//用path去保存data
[data writeToFile:path atomically:NO];
}
//例子3
NSArray*patharray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*docstr=[patharray objectAtIndex:0];
//path成员变量,文件路径-自动释放对象
path=[docstr stringByAppendingPathComponent:@"1.png"];
//创建按钮
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0.0, 0.0, 100, 100)];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
相关新闻>>
- 发表评论
-
- 最新评论 更多>>