Post not yet marked as solved
In my SwiftUI view, I try to load the image from data.
var body: some View {
Group{
if let data = model.detailImageData, let uiimage = UIImage(data: data) {// no memory issue
Image(uiImage: uiimage)
.resizable()
.scaledToFit()
}
}
}
But I want to get the HDR style of my image, so I use
if let data = model.detailImageData, let uiimage = UIImageReader.default.image(data:data){ //memory leaks!!!
When I change the data, the memory of the previous image is never freeed. finally caused my app to crash.
You can see it from the Instrument screenshot.
Post not yet marked as solved
I use this code to show the Image in HDR in SwiftUI
struct HDRImageView: UIViewRepresentable {
// Set up a common reader for all UIImage read requests.
static let reader: UIImageReader = {
var config = UIImageReader.Configuration()
config.prefersHighDynamicRange = true
return UIImageReader(configuration: config)
}()
let data:Data?
let enableHDR:Bool
func makeUIView(context: Context) -> UIImageView {
let view = UIImageView()
view.preferredImageDynamicRange = enableHDR ? .high : .standard
update(view)
// Set this view to fit itself to the parent view.
view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
view.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
view.setContentHuggingPriority(.required, for: .horizontal)
view.setContentHuggingPriority(.required, for: .vertical)
return view
}
func updateUIView(_ view: UIImageView, context: Context) {
update(view)
}
func update(_ view: UIImageView) {
autoreleasepool{//not working
if let data = data {
view.image = nil//set to nil first is not working
view.image = HDRImageView.reader.image(data: data)
} else {
view.image = nil
}
view.preferredImageDynamicRange = enableHDR ? .high : .standard
}
}
}
But when I update the input data, seems that the old image data can not be freeed.
After several changes, the app takes too much memory and crash.
I found it's the VM:ImageIO_Surface_Data and the VM_Image_IO take up the memory.
If I change the HDRImageView into a normal Image(uiimage:UIImage(data:)) It no longer have this issue.
Is it a memory leak? and how to solve this.
Update: I then tried using Image(_:cgImage), and it appear to be the same result.
Post not yet marked as solved
I have an idea that uses the IMU in the AirPods, but on the challenge website, I can't find any information about if it is allowed.