gpt4 book ai didi

objective-c - 如何释放UIImageJPEGRepresentation或UIImagePNGRepresentation生成的数据?

转载 作者:行者123 更新时间:2023-11-29 05:06:11 28 4
gpt4 key购买 nike

我有这样的问题:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSData *data;

NSString *file1 = [[NSBundle mainBundle] pathForResource:
[NSStringstringWithFormat:@"originimg_%d.jpg",i] ofType:nil]] ;

UIImage *image1 = [[UIImage alloc]initWithContentsOfFile:file1];
data = UIImageJPEGRepresentation(image, 0.7);
// do sth with data ...

[image1 release];
image1 = nil;
[pool drain];
pool = nil;
if(data)
NSLog(@"still exist");

我检查了数据是否仍然存在于内存中,(我预计在耗尽自动释放池后它会被删除)但它仍然存在:(。您知道如何删除该数据吗?

最佳答案

真的谢谢你,我测试了一下,确实如此。这是我的问题的概述:我的设备中有 132 个图像(~300 kb/1 个图像),现在我的目的是将每 2 个图像合并为 1 个大图像(水平方向并排)。这就是我所做的:

int index = 1;
for(int i = 1;i <= 132;i++)
{
if(i % 2 == 0 && i > 1)
{
NSString *file = [NSString stringWithFormat:@"%@img_%d.jpg",path2,index];

NSLog(@"index %d",index);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *data;
NSString *filename1 = [NSString stringWithFormat:@"originimg_%d.jpg",i];
NSString *filename2 = [NSString stringWithFormat:@"originimg_%d.jpg",i + 1];
NSString *file1 = [[NSBundle mainBundle] pathForResource:filename1 ofType:nil];
NSString *file2 = [[NSBundle mainBundle] pathForResource:filename2 ofType:nil];

UIImage *image1 = [[UIImage alloc]initWithContentsOfFile:file1];
UIImage *image2 = [[UIImage alloc]initWithContentsOfFile:file2];

UIImage *image = [self combineImages:image1 toImage:image2];
data = UIImageJPEGRepresentation(image, 0.7);
[data writeToFile:file atomically:NO];

[image1 release];
image1 = nil;
[image2 release];
image2 = nil;

[pool drain];
pool = nil;
[file release];
file = nil;
index++;
}
}

以及组合 2 个图像的函数

-(UIImage *)combineImages:(UIImage *)image1 toImage:(UIImage *)image2 
{
CGSize size;
size= CGSizeMake(768 * 2, 1024);
UIGraphicsBeginImageContext(size);

// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

// Draw image2
[image2 drawInRect:CGRectMake(image1.size.width, 0, image2.size.width, image2.size.height)];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
return resultingImage ;
}
  • 这是我的方式,但是当我在工具(分配)中运行时,它需要 303.4 mb :( 。你能建议我更好的方法吗?

关于objective-c - 如何释放UIImageJPEGRepresentation或UIImagePNGRepresentation生成的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5073395/

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