zoukankan      html  css  js  c++  java
  • UIImagePickerController之死因 .

    UIImagePickerController是我们最常用的组件之一,用它可以实现照相,选图片的功能。但是在照像的时候,如果后台开有很多应用,经常会出现crash的情况,是因为照出来的相片像素太高,一般是4M左右,照一张就会有4M内存占用,于是经常会出现memory warning, 然后系统回收内存,就使我们的应用crash。

    解决方法就是,在UIImagePickerControllerDelegate方法中启用一个线程来crop我们照出来的image,如下:

    1. - (void)imagePickerController:(UIImagePickerController *)picker  
    2.                     didFinishPickingMediaWithInfo:(NSDictionary *)info {  
    3.   
    4.   [[picker parentViewController] dismissModalViewControllerAnimated:YES];  
    5.   UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];  
    6.   [NSThread detachNewThreadSelector:@selector(useImage:) toTarget:self withObject:image];  
    7.   
    8. }  


    在useImage:方法是裁剪相片,使其占用更小的内存, 

     - (void)useImage:(UIImage *)image {  

    1.   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
    2. // Create a graphics image context   
    3.   CGSize newSize = CGSizeMake(320, 480);  
    4.   UIGraphicsBeginImageContext(newSize);  
    5. // Tell the old image to draw in this new context, with the desired   
    6. // new size   
    7.   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];  
    8. // Get the new image from the context   
    9.   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();  
    10. // End the context   
    11.   UIGraphicsEndImageContext();  
    12.   [userPhotoView setImage:newImage];  
    13.   [pool release];  
    14. }  
     

    这样做以后,虽然有memory warning也不至于crash.

    在此我分享一下另一个经验,如果我们的view都放在一个xib里,那么很可以造成内存警告,所以最好是一个xib对应一个view,当这个view需要的时候,才加载,不需要的时候就释放。避免hide这类的操作,因为hide其实还占有内存,只是不显示出来。

  • 相关阅读:
    可以链接不同源的资源的html元素(能实现跨域)
    触摸点位置
    浏览器各种高度
    koa 知识点
    next().value和next().done
    js中逻辑为false的8种情况
    Node.js热部署方式
    在package.json里面的script设置环境变量,区分开发及生产环境。注意mac与windows的设置方式不一样
    js事件冒泡和事件捕获
    阻止事件冒泡两种方式:event.stopPropagation();和return false;
  • 原文地址:https://www.cnblogs.com/fanjing/p/4519464.html
Copyright © 2011-2022 走看看