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:方法是裁剪相片,使其占用更小的内存,

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


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


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

参考资料:http://wiresareobsolete.com/2010/08/uiimagepickercontroller

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
struct VideoPicker: UIViewControllerRepresentable { @Environment(.presentationMode) private var presentationMode let sourceType: UIImagePickerController.SourceType // let onImagePicked: (UIImage) -> Void let onURLPicked: (URL) -> Void final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { @Binding private var presentationMode: PresentationMode private let sourceType: UIImagePickerController.SourceType private let onURLPicked: (URL) -> Void init(presentationMode: Binding<PresentationMode>, sourceType: UIImagePickerController.SourceType, onURLPicked: @escaping (URL) -> Void) { presentationMode = presentationMode self.sourceType = sourceType self.onURLPicked = onURLPicked } func imagePickerController( picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { // let uiImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage // onImagePicked(uiImage) if let url = info[.mediaURL] as? URL{ onURLPicked(url) } presentationMode.dismiss() } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { presentationMode.dismiss() } } func makeCoordinator() -> Coordinator { return Coordinator(presentationMode: presentationMode, sourceType: sourceType, onURLPicked: onURLPicked) } func makeUIViewController(context: UIViewControllerRepresentableContext<VideoPicker>) -> UIImagePickerController { let picker = UIImagePickerController() picker.sourceType = sourceType picker.delegate = context.coordinator picker.mediaTypes = ["public.movie"] return picker } func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<VideoPicker>) { } }这段代码获取的url中绝对路径不准确
05-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值