xcode UIImageJPEG 表示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8821536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIImageJPEGRepresentation
提问by Manuel
I have troubles finding a memory leak which then again causes my App to crash. It seems that the memory allocated for the JPEGRepresentation does not get released. This is even more curious because the NSData Object created by UIImageJPEGRepresentation is only about 300 kb big (dependend on the image), but the memory usage jumps up by about 3-5 megabyte per image at this stage.
我很难找到内存泄漏,然后再次导致我的应用程序崩溃。似乎没有释放为 JPEGRepresentation 分配的内存。这更奇怪,因为 UIImageJPEGRepresentation 创建的 NSData 对象只有大约 300 kb 大(取决于图像),但是在此阶段内存使用量每张图像增加了大约 3-5 MB。
This is the code
这是代码
QueuedObject* queuedObject = [[QueuedObject alloc] init];
[queuedObject setUrl:url];
QueuedObjectData* jsonQueuedData = [[QueuedObjectData alloc] init];
[jsonQueuedData setData:jsonData];
[jsonQueuedData setFilename:@"message.json"];
[jsonQueuedData setContentType:@"application/json"];
[jsonQueuedData setKeyValue:@"JSONMessage"];
[queuedObject addData:jsonQueuedData];
int i=1;
QueuedObjectData* imageData = [[QueuedObjectData alloc] init];
for(id file in files)
{
if(file!=nil)
{
[imageData setData:UIImageJPEGRepresentation(file, 0.8)];
[imageData setFilename:[NSString stringWithFormat:@"image%d.jpg",i]];
[imageData setContentType:@"image/jpeg"];
[imageData setKeyValue:@"image"];
[queuedObject addData:imageData];
i++;
}
}
[[UploadQueue sharedInstance] addObject:queuedObject];
[jsonQueuedData release];
[jsonData release];
[url release];
[imageData release];
[queuedObject release];
Maybe you have an idea to help me
也许你有什么想法可以帮助我
best regards Manuel
最好的问候曼努埃尔
回答by beryllium
Now you are using only one imageData
object in your cycle. So, you always create JPEG representation without deleting. Try this code:
现在您imageData
在循环中只使用一个对象。因此,您始终创建 JPEG 表示而不删除。试试这个代码:
for(id file in files)
{
if(file!=nil)
{
QueuedObjectData* imageData = [[QueuedObjectData alloc] init];
[imageData setData:UIImageJPEGRepresentation(file, 0.8)];
[imageData setFilename:[NSString stringWithFormat:@"image%d.jpg",i]];
[imageData setContentType:@"image/jpeg"];
[imageData setKeyValue:@"image"];
[queuedObject addData:imageData];
[imageData release];
i++;
}
}
Here you create object, use it and then delete.
在这里您创建对象,使用它然后删除。