gpt4 book ai didi

ios - Swift 4 中的 UIImagePickerController 内存泄漏 Xcode 9

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:59 51 4
gpt4 key购买 nike

在我的应用程序中,当我使用 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 点中,您说您必须使用委托(delegate)对象关闭图像选择器,在 Apple 的示例中,UIImagePickerDelegate 正在执行关闭操作。

问题是当您选择图像并使用它时,内存泄漏会浪费大约 21 MB 的内存。

没有内存泄漏的已用内存

enter image description here

内存泄漏的已用内存

enter image description here

内存泄漏

enter image description here

这是呈现 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.
})
}

这是委托(delegate)中关闭 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()
}
})
}

我仍在寻找解决方案,我们将不胜感激。

最佳答案

我遇到了同样的问题。很多其他人也是如此。追溯到 2008 年。非常疯狂。

不幸的是,我能找到的最佳答案是使用单例,这很糟糕。 [IE。有意保留 UIImagePickerController 的一个实例,这样您每次想要选择图像时都可以访问它。] 这是其他人的建议。

此外,我只花了一个小时用使用单例的代码写了这个答案,我就是无法避免内存泄漏 [虽然我认为我有一秒钟]。也许我只是做错了 - 请随意尝试。但我不会发布功能失调的代码作为答案。

最佳答案 [这就是我解决问题的方法] 是使用第三方 pod/库。我使用了 ImagePicker,它快速、快速、免费、漂亮,而且没有内存泄漏! [麻省理工学院许可证]

在这里查看: https://github.com/hyperoslo/ImagePicker

关于ios - Swift 4 中的 UIImagePickerController 内存泄漏 Xcode 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48734044/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com