5

I am facing a serious issue with UIImagePickerController in swift. After capturing 50-60 images continuously, the app crashed without logging any issue in fabric crashlytics. It seems to be a memory issue. I have tried the following solution.

@interface UIImagePickerController (Singleton)
   +(UIImagePickerController *) instance;
@end

@implementation UIImagePickerController (Singleton)
  +(UIImagePickerController *) instance{
   static UIImagePickerController *_instance;
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken,^{
     _instance=[[UIImagePickerController alloc] init];
   });
   return _instance
  }

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
    let image = UIImagePickerController.instance() //Singleton call used here
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.Camera
    image.allowEditing = true
    self.presentViewController(image,animated: true,completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){
    autoreleasepool{
      var image: UIImage? =info[UIImagePickerControllerOriginalImage] as! UIImage
      imageView.image = Utils.resizeAndCompress(image)
      image=nil
   } 
   self.dismissViewControllerAnimated(true,completion: nil)
}

Any idea what's wrong here? Your help will be appreciated. Thanks

class func resizeAndCompress(image: UIImage!) -> UIImage? {

    let size = image.size
    let maxLength: CGFloat = 640.0

    var newSize: CGSize
    if size.width > size.height {
        let scale = maxLength / size.width
        newSize = CGSizeMake(maxLength, size.height * scale)
    } else {
        let scale = maxLength / size.height
        newSize = CGSizeMake(size.width * scale, maxLength)
    }

    // This is the rect that we've calculated out and this is what is actually used below
    let rect = CGRectMake(0, 0, newSize.width, newSize.height)

    // Actually do the resizing to the rect using the ImageContext stuff
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    image.drawInRect(rect)
    let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

        return resizedImage

}

Solution for crashing It appears the Issue is with the iOS version. I have created my custom image picker control using AVVideoCapture. Now I am able to capture more than 1000 images.

Efren
  • 4,003
  • 4
  • 33
  • 75
  • 1
    put a breakpoint in didReceiveMemoryWarning method and see if it gets hit during the crash. If not, then it is not a memory issue. – Teja Nandamuri Jul 01 '16 at 13:01
  • What is the size of image returned by resizeAndCompress? – Teja Nandamuri Jul 01 '16 at 13:02
  • 1
    `image=nil` is one curious bit in your delegate method. Are you sure you are not storing images in memory? Also, why do you need the singleton pattern here? – NSNoob Jul 01 '16 at 13:05
  • It looks like he's trying to store way to much in the cache to me. – Sethmr Jul 01 '16 at 13:09
  • @teja-nandamuri, yes it hit the didReceiveMemoryWarning method. – Shyam Agarwal Jul 01 '16 at 13:16
  • @NSNoob I have implemented singleton pattern due to suggestion on stackoverflow on other posts. But the error is there with singleton or without singleton pattern – Shyam Agarwal Jul 01 '16 at 13:19
  • @Sethmr Can you please explain how it's storing in cache? – Shyam Agarwal Jul 01 '16 at 13:20
  • 1
    Can you show the method resizeAndCompress ? – Teja Nandamuri Jul 01 '16 at 13:20
  • @user2842495 Okay. It appears however that somehow the selected images remain your memory. This can cause crashes due to low memory because images take a lot of toll. Can you add the method requested by Teja so that we can see if you are retaining images somewhere or not? – NSNoob Jul 01 '16 at 13:24
  • @TejaNandamuri,NSNoob I have updated the question description. Please check – Shyam Agarwal Jul 01 '16 at 13:31
  • please refer to answer: http://stackoverflow.com/questions/11578934/memory-warning-and-crash-arc-how-to-identify-why-its-happening and this http://stackoverflow.com/questions/5860215/resizing-a-uiimage-without-loading-it-entirely-into-memory – Teja Nandamuri Jul 01 '16 at 13:36
  • Can you post your complete UIImagePickerController implementation? – Marco Pace Jul 01 '16 at 13:38
  • I don't know much about how the cache works honestly, but a couple things about your code just hit me as something that would cause a memory warning possibly. Sorry I am not more help. – Sethmr Jul 01 '16 at 13:48
  • Your imagePicker is storing the images in: `info[UIImagePickerControllerOriginalImage]` You already know this. That is why you force cast it to `UIImage` in your `didFinishPickingMediaWithInfo`. Get rid of it. You thought you `nil` it out by setting the reference to the image to `nil` but it didn't work. You have to `nil` it in the dictionary. You are storing a strong reference to every image that was ever picked. – Brandon Jul 01 '16 at 14:23
  • @Brandon How can we set nil in the dictionary. info dictionary is read only. – Shyam Agarwal Jul 02 '16 at 05:42
  • I have created a custom Image picker controller using AVVideoCapture. Now i am able to capture more than 1000 images. – Shyam Agarwal Aug 10 '16 at 10:58

0 Answers0