Received memory warning. Level=1 when showing a UIImagePickerController

Getting a "Received memory warning. Level=1" whenever I attempt to show a UIImagePickerController with a sourceType = UIImagePickerControllerSourceTypeCamera.

http://stackoverflow.com/questions/3099029/received-memory-warning-level-1-when-showing-a-uiimagepickercontroller/6871294#6871294


Solution:


It is not about how much memory your app has used, because it will probably happen even when you write a very simple app which have only one view with one button, clicking the button and then open camera. I have tested on iPhone 3GS, iPad 2 and iPod touch 3G. It only happened in iPhone 3GS. I found it will not happen anymore if you restart you device before you execute you app.

Another real solution is to comment the code, [super didReceiveMemoryWarning], in your viewController.

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

After lots of test on iPhone 3GS with iOS 4.3.2, I found the logic might like that: -> Open as much as app running on background -> Presenting a imagePicker of UIImagePickerController, clicking "Back" or "Save" from imagePicker -> ApplicationDelegate's method,applicationDidReceiveMemoryWarning:(UIApplication *)application, will be invoked -> Then ViewController's method, didReceiveMemoryWarning:, will be invoked -> Then viewDidUnload -> Then viewDidLoad

Then you could find some views have been released and the current view has been pointed to a unexpected one.

By default, [super didReceiveMemoryWarning] will run when ViewController'sdidReceiveMemoryWarning method is invoked. Commenting it, and viewDidUnload: andviewDidLoad: methods will not be invoked. It means the mem warning has been totally ignored. That's what we expected.



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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 getVideoURL(from assetIdentifier: String, completion: @escaping (URL?) -> Void) { let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: nil) guard let asset = fetchResult.firstObject else { completion(nil) return } let options = PHVideoRequestOptions() options.version = .original PHImageManager.default().requestAVAsset(forVideo: asset, options: options) { avAsset, , _ in guard let avAsset = avAsset else { completion(nil) return } let url = (avAsset as? AVURLAsset)?.url completion(url) } } 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、付费专栏及课程。

余额充值