writeImageDataToSavedPhotosAlbum 在几次使用后显着变慢
我使用 api“writeImageDataToSavedPhotosAlbum”将图像保存到 iPhone 的相册中。然而,在多次使用(例如 4 次)之后,每次保存的时间开始增加大约两倍。
我创建了一个测试方法来重现它,每次都会保存完全相同的图像。方法如下:
-(IBAction)testButton {
NSData *data = [NSData dataWithContentsOfFile:[[self photosDirectory] stringByAppendingPathComponent:[[self contentsOfPhotoDirectory] objectAtIndex:0]]];
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
__block NSDate *date = [[NSDate date] retain];
[al writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(@"Saving Time: %g", [[NSDate date] timeIntervalSinceDate:date]);
[date release];
}];
[al release];
}
在刚重启的 iPhone 4 上,我得到以下保存时间。
2011-06-01 21:23:13.641 myapp[95:707] Saving Time: 5.30819
2011-06-01 21:23:17.101 myapp[95:707] Saving Time: 1.5311
2011-06-01 21:23:21.916 myapp[95:707] Saving Time: 2.52412
2011-06-01 21:23:25.974 myapp[95:707] Saving Time: 2.85623
2011-06-01 21:23:32.275 myapp[95:707] Saving Time: 4.93484
2011-06-01 21:23:42.024 myapp[95:707] Saving Time: 7.93288
2011-06-01 21:24:00.317 myapp[95:707] Saving Time: 15.8561
2011-06-01 21:24:33.199 myapp[95:707] Saving Time: 29.7571
怎么了?
I save images to the iPhone's photo album using the api "writeImageDataToSavedPhotosAlbum". However, after several uses (such as 4), it begins to take approximately twice as long each time to save.
I have created a test method to reproduce it, where it saves the exact same image each time. The method is as follows:
-(IBAction)testButton {
NSData *data = [NSData dataWithContentsOfFile:[[self photosDirectory] stringByAppendingPathComponent:[[self contentsOfPhotoDirectory] objectAtIndex:0]]];
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
__block NSDate *date = [[NSDate date] retain];
[al writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(@"Saving Time: %g", [[NSDate date] timeIntervalSinceDate:date]);
[date release];
}];
[al release];
}
On a freshly restarted iPhone 4, I get the following save times.
2011-06-01 21:23:13.641 myapp[95:707] Saving Time: 5.30819
2011-06-01 21:23:17.101 myapp[95:707] Saving Time: 1.5311
2011-06-01 21:23:21.916 myapp[95:707] Saving Time: 2.52412
2011-06-01 21:23:25.974 myapp[95:707] Saving Time: 2.85623
2011-06-01 21:23:32.275 myapp[95:707] Saving Time: 4.93484
2011-06-01 21:23:42.024 myapp[95:707] Saving Time: 7.93288
2011-06-01 21:24:00.317 myapp[95:707] Saving Time: 15.8561
2011-06-01 21:24:33.199 myapp[95:707] Saving Time: 29.7571
What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题可能是您正在使用内存而不是释放它,因此稍后保存会生成大量内存警告...这正是 Time Profiler 和 Object Alloc 工具应该能够帮助您解决的问题,弄清楚为什么它在几张图像后就变慢了。
在我自己的应用程序中,我认为在重复拍摄后我并没有看到那么大的减速。
Possibly the problem is that you are using memory and not freeing it, so later saves generate a lot of memory warnings... this is exactly the kind of thing the Time Profiler and Object Alloc instruments should be able to help you with, figuring out why it slows after just a few images.
In my own app I do not think I saw that much of a slowdown after repeated shots.
使用ARC进行内存管理。我正在使用这种方法保存在我正在编写的相机应用程序中,并且没有像您这样的问题。
Use ARC for memory management. I'm using this method of saving in a camera app I'm writing and have no issues like yours.