内存警告 UIImagepickerController IOS 7

2023-11-24

任何人都可以帮助我解决这个问题吗?我对 Objective C 和 iOS 有点陌生。我一直在研究它,但我不知道如何解决这个问题,我的应用程序非常简单,它只启动相机拍照并通过电子邮件将它们发送到我们的服务器。这段代码在 iOS6 中运行得很好。

当我拍照时,我的内存随着每次屏幕捕获而堆增长,我收到“收到内存警告”,最后 - 由于内存压力而终止。 -

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{

[self.popoverController2 dismissPopoverAnimated:true];
NSString *mediaType = [info
                       objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    _image = [info
                      objectForKey:UIImagePickerControllerOriginalImage];

   _image = [self fixrotation:_image]; //<----- increased memory when UIImageWriteToSavedPhotosAlbum is uncommented IF is comment it doesn't increased memory but after some pictures I start to get "Received Memory Warning" message until the app Crash.

    if (_newMedia){
       UIImageWriteToSavedPhotosAlbum(_image,
                                       self,@selector(image:finishedSavingWithError:contextInfo:),
                                       nil);
    [self dismissViewControllerAnimated:NO completion:nil];
    [self performSegueWithIdentifier:@"SeleccionadoCameraR" sender:self];


    }else{
        [self performSegueWithIdentifier:@"SeleccionadoCameraR" sender:self];

    }

}

}

- (UIImage *)fixrotation:(UIImage *)image{


if (image.imageOrientation == UIImageOrientationUp) return image;
CGAffineTransform transform = CGAffineTransformIdentity;

switch (image.imageOrientation) {
    case UIImageOrientationDown:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;

    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;

    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, image.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationUpMirrored:
        break;
}

switch (image.imageOrientation) {
    case UIImageOrientationUpMirrored:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;

    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationDown:
    case UIImageOrientationLeft:
    case UIImageOrientationRight:
        break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                         CGImageGetBitsPerComponent(image.CGImage), 0,
                                         CGImageGetColorSpace(image.CGImage),
                                         CGImageGetBitmapInfo(image.CGImage));


CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        // Grr...
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
        break;

    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage); //when I use instruments it shows that My VM is because of this 
        break;
}

// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);//also this line in Instruments
UIImage *img = [UIImage imageWithCGImage:cgimg];


CGContextRelease(ctx);
CGImageRelease(cgimg);


return img;


 }

大概是内存管理。我会感谢你的帮助


你正走在正确的轨道上fixRotation方法。但是,您还应该resize图片。否则,图像将会很大,大约 30 MB(取决于设备)。

查看这篇博文关于如何正确调整图像大小。具体来说,UIImage您想要的类别文件是这些:

UIImage+Resize.h

UIImage+Resize.m

在后台线程上执行此操作也是一个好主意。像这样的东西

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Dismiss the image picker first to free its memory
    [self dismissViewControllerAnimated:YES completion:nil];

    UIImage *originalImage = info[UIImagePickerControllerOriginalImage];

    if (!originalImage)
        return;

    // Optionally set a placeholder image here while resizing happens in background

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Set desired maximum height and calculate width
        CGFloat height = 640.0f;  // or whatever you need
        CGFloat width = (height / originalImage.size.height) * originalImage.size.width;

        // Resize the image
        UIImage * image = [originalImage resizedImage:CGSizeMake(width, height) interpolationQuality:kCGInterpolationDefault];

        // Optionally save the image here...

        dispatch_async(dispatch_get_main_queue(), ^{
            // ... Set / use the image here...
        });           
    });
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

内存警告 UIImagepickerController IOS 7 的相关文章

随机推荐

  • IIS8.5自动更改物理路径属性

    我在 IIS8 5 中的默认网站下添加了几个应用程序 仅端口 80 IP 上的 http 它们指向不同的物理路径 不是嵌套的 并且在添加此类应用程序后效果很好 但过了一会儿 其中一个应用程序的物理路径会自动更改为另一个应用程序的物理路径 为
  • 在 Javascript 闭包中访问“this”

    这更像是一次健全性检查 我发现在 Javascript 中使用闭包时 我经常使用以下模式从函数内访问封闭类 MyClass prototype delayed foo function var self this setTimeout fu
  • Grep 并打印返回引用

    我有这个 iptable 日志 Feb 25 10 32 48 XXX 414645 555838 FW DEN TCP IN eth0 OUT MAC XYZ SRC 1 1 1 1 DST 2 2 2 2 LEN 40 TOS 0x00
  • 如何求椭圆的半轴长度?在R中

    我有这组 x 和 y 坐标 x lt c 1 798805 2 402390 2 000000 3 000000 1 000000 y lt c 0 3130147 0 4739707 0 2000000 0 8000000 0 10000
  • WCF 中出现带有“无法识别序列”消息的 CommunicationException

    我在使用 WCF 服务时收到 CommunicationException 消息是 远程端点不再识别该序列 这很可能是由于远程端点上的中止造成的 wsrm Identifier 的值不是已知的序列标识符 可靠会话出现故障 调用合约方法后不久
  • 包括换行符在内的任何字符 - Java Regex

    我想这可能是 n 但这似乎不起作用 点不能在字符类内部使用 查看选项图案 DOTALL Pattern DOTALL启用 dotall 模式 在 dotall 模式下 表达式 匹配任何字符 包括行终止符 默认情况下 此表达式不匹配行终止符
  • 调用 dlclose(NULL) 安全吗?

    当我经过某个地方时 我经历了一次车祸null指向dlclose 我应该在调用之前检查 null 吗dlclose POSIX 对此没有任何说明 http pubs opengroup org onlinepubs 7908799 xsh d
  • 每行具有动态列数的 GridView

    如何在 a 的帮助下重新创建以下视图GridView 列表中的项目数是动态的 我猜想这不是单个GridView而是多个Layout的组合 只需制作一个 LinearLayout 并根据内容决定您想要在一行中使用哪种布局
  • 尝试通过 FTP 传输带有特殊字符的文件

    我有代码 FtpWebRequest request FtpWebRequest FtpWebRequest Create url request Method WebRequestMethods Ftp DownloadFile requ
  • 如何将联系人或短信作为 .xml 文件或 .csv 文件备份到 SD 卡并在以后恢复

    我正在尝试在我的应用程序中开发一项功能 将短信和联系人以 xml 或 csv 格式备份到 SD 卡 并在以后恢复 所以请任何人给我一些建议或一些示例代码或与此相关的任何资源链接 提前致谢 public ArrayList
  • Django 中 Pisa 的 pdf 生成未渲染 CSS

    我使用 Pisa 从 HTML 生成 pdf 文件 def fetch resources uri rel path os path join settings MEDIA ROOT uri replace settings MEDIA U
  • Sinatra 如何定义和调用 get 方法?

    我很好奇这东西是如何工作的 在需要 sinatra 之后 然后我可以在顶级范围内调用 get 深入研究源代码后 我发现了这个 get 结构 module Sinatra class lt lt self def get end end en
  • strstr 的纯字节版本?

    是否有一个 strstr 版本可以在可能包含空字符的固定长度内存上工作 我可以这样表达我的问题 strncpy 与 memcpy 的关系与 strstr 的关系相同 memmem 不幸的是它是 GNU 特定的而不是标准 C 但是 它是开源的
  • 通过名称模糊匹配创建唯一 ID(通过使用 R 的 agrep)

    使用 R 我尝试匹配按年份和城市构建的数据集中的人名 由于一些拼写错误 不可能进行精确匹配 因此我尝试使用 agrep 来模糊匹配名称 数据集的样本块的结构如下 df lt data frame matrix c 1200013 12000
  • 你能用 Go 在内存中“固定”一个对象吗?

    我有一个 Go 对象 我希望其在内存中的地址保持不变 在 C 中 我们可以固定对象在内存中的位置 Go 有办法做到这一点吗 您保留引用的对象不会移动 没有句柄或间接寻址 您获得的地址是永久的 From 文档 请注意 与 C 不同 返回 a
  • 有什么更快的方法可以找到“幸运三元组”的数量?

    我正在研究一个代码挑战问题 寻找幸运三元组 幸运三重 被定义为 在列表中lst 对于三元组的任意组合 lst i lst j lst k where i lt j lt k where lst i divides lst j and lst
  • 具有 ECDHE 密钥和证书的服务器无法正常工作

    我使用下面的 server c 源代码 我生成了 sinful host cert pem sinful host key 如此处所述 椭圆曲线 CA 指南 运行程序时出现以下错误 140722397161136 错误 10071065 椭
  • 在 ggplot 中展开分类 x 轴

    我不知道如何使用expand within scale x discrete 扩展分类 x 轴 以便将标签放置在点的右侧不会脱离绘图 我知道如果 x 是数字 我可以简单地调整 x 的最大限制 以便所有点都向左移动 但是 我无法弄清楚如何使用
  • SQL Server 导入向导将 NULL 视为文字字符串“NULL”

    当我尝试导入 csv以逗号分隔的平面文件转换为Microsoft SQL server 2008R2 64 bit instance for string列aNULL原始数据变成文字字符串 NULL 并在一个numeric列我收到导入错误
  • 内存警告 UIImagepickerController IOS 7

    任何人都可以帮助我解决这个问题吗 我对 Objective C 和 iOS 有点陌生 我一直在研究它 但我不知道如何解决这个问题 我的应用程序非常简单 它只启动相机拍照并通过电子邮件将它们发送到我们的服务器 这段代码在 iOS6 中运行得很