Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is pretty memory intensive. The base64 <code>string</code>, the <code>byte[]</code> array, the <code>NSData</code>, the <code>UIImage</code>, the second <code>NSData</code>, the second <code>byte[]</code> array. That's many times the required memory of the image you're working on.</p> <p>Depending on how the code is called (e.g. a loop over a bunch of images) you might be allocating memory faster than the GC is able to reclaim it. At some point iOS will trigger a memory warning (but that won't help in a loop) and it will kill you if memory usage is not rapidly reduced.</p> <p>You can easily lower your memory requirement by disposing of every <code>IDisposable</code> instance your code create. Right now you're disposing only one of the two <code>NSData</code> instances you create and you're not disposing of the <code>UIImage</code>. You could dispose of all of them like this:</p> <pre><code>byte[] encodedDataAsBytes = Convert.FromBase64String ( base64String ); using (var nsd1 = NSData.FromArray ( encodedDataAsBytes )) using (var img = UIImage.LoadFromData ( nsd1 )) using (NSData data = img.AsPNG ()) { byte[] returnBytes2 = new byte [data.Length]; Marshal.Copy (data.Bytes, returnBytes2, 0, (int) data.Length); return returnBytes2; } </code></pre> <p>Next, what's the purpose of the code later on ? i.e. what are you doing with the <code>byte[]</code> containing the PNG image ? It's not very useful by itself, but if you're only saving it to disk then you could do so from the <code>NSData</code> and skip the last <code>byte[]</code> allocation. </p> <p>Other tricks, usage dependent, could also be applied (e.g. reusing the second <code>byte[]</code> buffer when possible) but we would need to know more about how the code is being used.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload