gpt4 book ai didi

iphone - 从照片库加载图像并按日期排序的内存问题

转载 作者:行者123 更新时间:2023-12-01 16:50:43 24 4
gpt4 key购买 nike

我正在从我的照片库中获取图像,并根据 Assets 的日期属性,将这些图像排列在不同的文件夹中。我想做的是从照片库中获取图像,根据日期存储它们。例如如果获取的图像来自 4 月 23 日,请将它们存储在“April”文件夹中。我为此使用 WSAssetPickerController。请帮助我解决内存问题,因为我的应用程序由于设备上的内存问题而崩溃,这在模拟器中运行良好。

- (void)storePhotos:(NSArray *)assets {

int index = 0;

for (ALAsset *asset in assets) {

NSLog(@"Current asset :%@ ",asset);
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMMM dd, yyyy"];

NSString *photoDate = [format stringFromDate:[asset valueForProperty:ALAssetPropertyDate]];

resourceDocPath = [resourceDocPath stringByAppendingPathComponent:photoDate];

// Create directory & Check directory
BOOL checkDir = [self createDocumentDirectory:resourceDocPath fileName:asset.defaultRepresentation.filename];

if (!checkDir) {
continue;
}

// Write image
NSString *writablePhotoPath = [resourceDocPath stringByAppendingPathComponent:asset.defaultRepresentation.filename];
[self writeImage:writablePhotoPath WithImage:asset.defaultRepresentation.fullScreenImage];

index++;
}
}

- (void)writeImage:(NSString*)pstrPhotoPath WithImage:(CGImageRef)pImage {

if (![[NSFileManager defaultManager] fileExistsAtPath:pstrPhotoPath]){

UIImage *image = [[UIImage alloc] initWithCGImage:pImage];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:pstrPhotoPath atomically:YES];

}
}

- (BOOL)createDocumentDirectory:(NSString*) pCurrentFolder fileName:(NSString*) pStrFileName {

if (![[NSFileManager defaultManager] fileExistsAtPath:pCurrentFolder]){

if ([self checkPhotoIsExistInDocument:pStrFileName]) {
return FALSE;
}

NSError* error;
if ([[NSFileManager defaultManager] createDirectoryAtPath:pCurrentFolder withIntermediateDirectories:NO attributes:nil error:&error]) {
// success
return TRUE;
} else {
NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]);
NSAssert( FALSE, @"Failed to create directory maybe out of disk space?");
return TRUE;
}
return TRUE;
} else {
return TRUE;
}
}

- (BOOL)checkPhotoIsExistInDocument:(NSString*) pStrImageName {

NSString *bundleRoot = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
NSFileManager *manager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot];

NSString *filename;

while ((filename = [direnum nextObject] )) {
// NSLog(@"%@", filename);
if ([filename hasSuffix:pStrImageName]) {
return TRUE;
}
}

return FALSE;
}

最佳答案

您在项目中使用 ARC 吗?因为您还没有释放任何正在分配和初始化的对象。

resourceDocPath
format
image
bundleRoot

应在使用结束后立即释放。此外,如果您可以提供应用程序崩溃的日志,那将非常有用。

关于iphone - 从照片库加载图像并按日期排序的内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16162506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com