# 'OutofMemoryError' occurs when using Mat(uiImage:)

**URL:** <https://forum.opencv.org/t/outofmemoryerror-occurs-when-using-mat-uiimage/14690>\
**Category:** C++\
**Tags:** apple\
**Created:** [September 29, 2023, 8:10am UTC](https://forum.opencv.org/t/outofmemoryerror-occurs-when-using-mat-uiimage/14690 "2023-09-29T08:10:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fumiya](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/fumiya/32/8804_2.png) [@fumiya](https://forum.opencv.org/u/fumiya)\
**Post date:** [September 29, 2023, 8:10am UTC](https://forum.opencv.org/t/outofmemoryerror-occurs-when-using-mat-uiimage/14690/1 "2023-09-29T08:10:50Z")

</div>

We are using the iOS framework.  
When calling Mat(uiImage:) several times consecutively, the following error occurs

```markdown
libc++abi: terminating with uncaught exception of type cv::Exception: OpenCV(4.5.5) /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 31961088 bytes in function 'OutOfMemoryError'

```

For example, the same error occurs with the following code

```markdown
for i in 0..<1000 {
   let testBundle = Bundle(for: type(of: self))
   let imagePath = testBundle.path(forResource: "XXXX", ofType: "jpeg")
   let image = UIImage(contentsOfFile:imagePath!)!
   var mat = Mat(uiImage: image)
}

```

We suspect that the memory of Mat is not being released.

Do you know the cause of these issues?  
And how can we resolve this?

---

<div class="post-metadata">

**Author:** ![Kumar\_Ranjan\_Kamila](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kumar_ranjan_kamila/32/9618_2.png) [@Kumar\_Ranjan\_Kamila](https://forum.opencv.org/u/Kumar_Ranjan_Kamila)\
**Post date:** [January 29, 2024, 10:12am UTC](https://forum.opencv.org/t/outofmemoryerror-occurs-when-using-mat-uiimage/14690/2 "2024-01-29T10:12:38Z")

</div>

Hii [fumiya](https://forum.opencv.org/u/fumiya) , Can you please help me converting an UIImage to Mat type using opencv and vice versa ?
