-(UIImage *) getImageFromURL:(NSString *)fileURL {
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
-(BOOL) saveImage:(UIImage *)image withFileName:(NSString *)imageName
{
NSString * directoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * extension = @"jpg";
if ([[extension lowercaseString] isEqualToString:@"png"])
{
[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
}
else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"])
{
[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
}
else
{
NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
return FALSE;
}
return TRUE;
}
for(int i =0; i<[imageNameArray count]; i++)
{
UIImage *imageFromURL = [self getImageFromURL:imgURL];
res = [self saveImage:imageFromURL withFileName:imgName];
}
您好我使用上面的代码将大量图片从URL存储到Application Document。
它完美无缺,但在尝试存储更多图像时会返回内存错误。
失败(错误代码= 3) 错误:无法分配区域 在malloc_error_break中设置断点以进行调试
任何人都可以澄清如何解决这个问题吗?
另外我怀疑文档目录的数据是否占用RAM内存?
如何将300多张图像加载到没有内存错误的应用程序文档中?
先谢谢..!
答案 0 :(得分:0)
缩小图像的压缩尺寸以包含更多图像。而不是 1.0 ,使用 0.5 作为压缩值。
[UIImageJPEGRepresentation(image, 0.5) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];