iOS内存泄漏,因为应用程序文档中存储了更多图像 -(UIImage*)getImageFromURL:(NSString*)文件URL{ UIImage*结果; NSData*data=[NSData data WITHCONTENTSOFURL:[NSURL URLWithString:fileURL]]; 结果=[UIImage imageWithData:data]; 返回结果; } -(BOOL)saveImage:(UIImage*)带文件名的图像:(NSString*)图像名 { NSString*directoryPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)对象索引:0]; NSString*扩展名=@“jpg”; if([[extension lowercaseString]isEqualToString:@“png”]) { [UIImagePNGRPresentation(image)WriteFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@“%@.%@”,imageName,@“png”]]选项:NSAtomicWrite错误:nil]; } else if([[extension lowercaseString]IsequalString:@“jpg”]| |[[extension lowercaseString]IsequalString:@“jpeg”]) { [UIImageJPEGresentation(image,1.0)writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@“%@.%@”,imageName,@“jpg”]]选项:NSAtomicWrite错误:nil]; } 其他的 { NSLog(@“图像保存失败\n无法识别张力:(@),使用(PNG/JPG)”,扩展名); 返回FALSE; } 返回TRUE; } 对于(int i=0;i
减小图像的压缩大小以包含更多的图像。使用0.5作为压缩值,而不是1.0iOS内存泄漏,因为应用程序文档中存储了更多图像 -(UIImage*)getImageFromURL:(NSString*)文件URL{ UIImage*结果; NSData*data=[NSData data WITHCONTENTSOFURL:[NSURL URLWithString:fileURL]]; 结果=[UIImage imageWithData:data]; 返回结果; } -(BOOL)saveImage:(UIImage*)带文件名的图像:(NSString*)图像名 { NSString*directoryPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)对象索引:0]; NSString*扩展名=@“jpg”; if([[extension lowercaseString]isEqualToString:@“png”]) { [UIImagePNGRPresentation(image)WriteFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@“%@.%@”,imageName,@“png”]]选项:NSAtomicWrite错误:nil]; } else if([[extension lowercaseString]IsequalString:@“jpg”]| |[[extension lowercaseString]IsequalString:@“jpeg”]) { [UIImageJPEGresentation(image,1.0)writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@“%@.%@”,imageName,@“jpg”]]选项:NSAtomicWrite错误:nil]; } 其他的 { NSLog(@“图像保存失败\n无法识别张力:(@),使用(PNG/JPG)”,扩展名); 返回FALSE; } 返回TRUE; } 对于(int i=0;i,ios,ipad,memory-leaks,Ios,Ipad,Memory Leaks,减小图像的压缩大小以包含更多的图像。使用0.5作为压缩值,而不是1.0 -(UIImage *) getImageFromURL:(NSString *)fileURL { UIImage * result; NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]; result = [UIImage imageWith
-(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];
}
不要在主线程中循环,而是使用不同的线程或使用块来写入更多图像?UIImageJPEGresentation(图像,0.5)是否会降低图像质量?是的,但在iphone屏幕上不明显-@Prabhu
[UIImageJPEGRepresentation(image, 0.5) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];