【问题标题】:UIImagePickerController ImageIO_PNG takes massive memoryUIImagePickerController ImageIO_PNG 占用大量内存
【发布时间】:2014-05-29 20:46:56
【问题描述】:

虽然我会在 UIImagePickerController 完成拍照后调整图像大小,但我的仪器配置文件显示每次拍照时调用 ImageIO_PNG 都会占用大量内存 (40 MB+)。这是我的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  {
    @autoreleasepool {
        if (myImageView.image == nil) {

            myImageView.contentMode = UIViewContentModeScaleAspectFill;

            UIImage *topImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

            UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
            CGRect rect = CGRectMake(0,0,320,440);

            UIGraphicsBeginImageContext( rect.size );
            // use the local image variable to draw in context
            [topImage drawInRect:rect];
            UIImage *topResized = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            myImageView.image = topResized;
            image = nil;
            info = nil;

            [picker dismissViewControllerAnimated:NO completion:nil];
            [picker removeFromParentViewController];

【问题讨论】:

  • 一方面,在这段代码的底部,topImage、info 和picker 仍然被引用,所以它们仍然会被锁定在内存中。不知道你为什么要获取 image 和 topImage (在这种情况下它们是同一个对象的别名)

标签: ios memory png uiimagepickercontroller


【解决方案1】:

从您的代码中删除可能有帮助的以下行:

image = nil;
info = nil;

【讨论】:

  • 您能否简要解释一下为什么它可能会有所帮助?
  • as image 将内存指向 image if stored 。如果我们给它 nil ,内存地址就会丢失。那个形象留在记忆中。就像我们将图像设为“nil”一样,它不会释放任何内容。释放就像 [nil release];
猜你喜欢
  • 2014-05-11
  • 2011-02-27
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多