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.