在我的应用程序中,当我使用UIImagePickerController
,我以为是我的应用程序的问题,但是在搜索解决方案时我找到了Apple的示例,并且我还发现该示例具有相同的内存泄漏。
您可以在以下 URL 中找到该示例。
https://developer.apple.com/library/content/samplecode/PhotoPicker/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010196
根据UIImagePickerController
文档:
https://developer.apple.com/documentation/uikit/uiimagepickercontroller
在第 5 点中,您说您必须使用委托对象来关闭图像选择器,在 Apple 的示例中UIImagePickerDelegate
正在解雇。
问题在于,当您选择图像并使用它时,内存泄漏会浪费大约 21 MB 的内存。
使用的内存没有内存泄漏
内存泄漏的已用内存
内存泄漏
这是呈现的代码UIImagePickerController
:
@IBAction func showImagePickerForPhotoPicker(_ sender: UIBarButtonItem) {
showImagePicker(sourceType: UIImagePickerControllerSourceType.photoLibrary, button: sender)
}
fileprivate func showImagePicker(sourceType: UIImagePickerControllerSourceType, button: UIBarButtonItem) {
// If the image contains multiple frames, stop animating.
if (imageView?.isAnimating)! {
imageView?.stopAnimating()
}
if capturedImages.count > 0 {
capturedImages.removeAll()
}
imagePickerController.sourceType = sourceType
imagePickerController.modalPresentationStyle =
(sourceType == UIImagePickerControllerSourceType.camera) ?
UIModalPresentationStyle.fullScreen : UIModalPresentationStyle.popover
let presentationController = imagePickerController.popoverPresentationController
presentationController?.barButtonItem = button // Display popover from the UIBarButtonItem as an anchor.
presentationController?.permittedArrowDirections = UIPopoverArrowDirection.any
if sourceType == UIImagePickerControllerSourceType.camera {
// The user wants to use the camera interface. Set up our custom overlay view for the camera.
imagePickerController.showsCameraControls = false
// Apply our overlay view containing the toolar to take pictures in various ways.
overlayView?.frame = (imagePickerController.cameraOverlayView?.frame)!
imagePickerController.cameraOverlayView = overlayView
}
present(imagePickerController, animated: true, completion: {
// Done presenting.
})
}
这是委托中用于驳回的代码UIImagePickerController
:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage]
capturedImages.append(image as! UIImage)
if !cameraTimer.isValid {
// Timer is done firing so Finish up until the user stops the timer from taking photos.
finishAndUpdate()
} else {
dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: {
// Done cancel dismiss of image picker.
})
}
fileprivate func finishAndUpdate() {
dismiss(animated: true, completion: { [weak self] in
guard let `self` = self else {
return
}
if `self`.capturedImages.count > 0 {
if self.capturedImages.count == 1 {
// Camera took a single picture.
`self`.imageView?.image = `self`.capturedImages[0]
} else {
// Camera took multiple pictures; use the list of images for animation.
`self`.imageView?.animationImages = `self`.capturedImages
`self`.imageView?.animationDuration = 5 // Show each captured photo for 5 seconds.
`self`.imageView?.animationRepeatCount = 0 // Animate forever (show all photos).
`self`.imageView?.startAnimating()
}
// To be ready to start again, clear the captured images array.
`self`.capturedImages.removeAll()
}
})
}
我仍在寻找解决方案,任何帮助将不胜感激。