/// <summary> /// Create a <see cref="NSImage"/> from a stream. /// </summary> /// <param name="stream">The stream.</param> /// <returns>An autoreleased <see cref="NSImage"/> instance</returns> public static NSImage ImageFromStream(Stream stream) { NSImage result = null; if (stream != null) { byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, (int)stream.Length); NSData data = new NSData(buffer); result = new NSImage(data); data.Release(); result.Autorelease(); } return result; }
/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param> /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"/> to use as the current culture.</param> /// <param name="value">The <see cref="T:System.Object"/> to convert.</param> /// <returns> /// An <see cref="T:System.Object"/> that represents the converted value. /// </returns> /// <exception cref="T:System.NotSupportedException"> /// The conversion cannot be performed. /// </exception> public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) { // As pool are scoped, it does not hurt if we create ours during the method call. using (NSAutoreleasePool pool = new NSAutoreleasePool()) { byte[] rawData = value as byte[]; if (rawData == null) { return base.ConvertFrom (context, culture, value); } // The image creation will be based on a NSData instance that wraps the byte array NSData data = new NSData (rawData); NSImage image = new NSImage (data); data.Release (); return image; // TODO: Find a way to avoid the memory leak as we do not release the instance... } }