示例1:正常写法 NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"photo" ofType:@"jpg"]];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//写入100张图片到沙盒中,观察内存
for (int i=0; i<100; i++) {
NSString *photoPath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"photo_%zi",i]];
UIImage *image = [[UIImage alloc] initWithData:data];
[UIImagePNGRepresentation(image) writeToFile:photoPath atomically:YES];
}
示例二:写完指向nil NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"photo" ofType:@"jpg"]];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
for (int i=0; i<100; i++) {
NSString *photoPath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"photo_%zi",i]];
UIImage *image = [[UIImage alloc] initWithData:data];
[UIImagePNGRepresentation(image) writeToFile:photoPath atomically:YES];
image=nil;
}示例三:使用自动释放池释放NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"photo" ofType:@"jpg"]];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
for (int i=0; i<100; i++) {
NSString *photoPath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"photo_%zi",i]];
@autoreleasepool {
UIImage *image = [[UIImage alloc] initWithData:data];
[UIImagePNGRepresentation(image) writeToFile:photoPath atomically:YES];
}
}输出结果:问题:
1.即使使用了autoreleasepool,写入过程内存最高值还是很高,手机程序可能崩溃,怎么解决?
2.指向nil,不是将该指向该对象的指针指向空,但实际该对象的内存还存在,并没释放掉?
3.运行前17.7MB,运行后最低21MB,多出来的3MB哪里来?
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//写入100张图片到沙盒中,观察内存
for (int i=0; i<100; i++) {
NSString *photoPath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"photo_%zi",i]];
UIImage *image = [[UIImage alloc] initWithData:data];
[UIImagePNGRepresentation(image) writeToFile:photoPath atomically:YES];
}
示例二:写完指向nil NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"photo" ofType:@"jpg"]];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
for (int i=0; i<100; i++) {
NSString *photoPath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"photo_%zi",i]];
UIImage *image = [[UIImage alloc] initWithData:data];
[UIImagePNGRepresentation(image) writeToFile:photoPath atomically:YES];
image=nil;
}示例三:使用自动释放池释放NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"photo" ofType:@"jpg"]];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
for (int i=0; i<100; i++) {
NSString *photoPath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"photo_%zi",i]];
@autoreleasepool {
UIImage *image = [[UIImage alloc] initWithData:data];
[UIImagePNGRepresentation(image) writeToFile:photoPath atomically:YES];
}
}输出结果:问题:
1.即使使用了autoreleasepool,写入过程内存最高值还是很高,手机程序可能崩溃,怎么解决?
2.指向nil,不是将该指向该对象的指针指向空,但实际该对象的内存还存在,并没释放掉?
3.运行前17.7MB,运行后最低21MB,多出来的3MB哪里来?
解决方案 »
- clash of clans 用手指拖动房子,改变房子的位置,碰到别的房子会变红,可以放置的地方放置下来会变绿,想实现这个效果,没思路,怎么办
- 关于解析Json数据类型的问题。
- 基础问题
- 别人写好的项目,我如何能加载到自己的电脑上运行?菜鸟级问题,请帮忙……
- UIView transform 缩放,后移动
- 请问project中的provisioning profile 究竟有什么用?
- 请问这个app的导航栏如何实现,请高手给点思路。多谢!
- iOS屏幕适配问题
- 从xib中加载的view上面的label的无法赋值,是怎么回事?
- 新手求助!关Thread 1: signal SIGABRT的错误
- 环信群聊里面发送名片,发送分享
- iOS 圆形全景图怎么实现的?
第三种的 autoreleasepool 只包含了一小部分代码, 其他的难道不需要吗?
NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"photo" ofType:@"jpg"]];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
for (int i=0; i<100; i++) {
NSString *photoPath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"photo_%zi",i]];
UIImage *image = [[UIImage alloc] initWithData:data];
NSData *data1 = UIImagePNGRepresentation(image);
[data1 writeToFile:photoPath atomically:YES];
data1 = nil;
image=nil;
}
data = nil