8
8
using osu . Framework . IO . Stores ;
9
9
using osu . Framework . Platform . Apple ;
10
10
using SixLabors . ImageSharp ;
11
+ using SixLabors . ImageSharp . Memory ;
11
12
12
13
namespace osu . Framework . Platform . MacOS
13
14
{
@@ -20,19 +21,21 @@ public MacOSTextureLoaderStore(IResourceStore<byte[]> store)
20
21
21
22
protected override unsafe Image < TPixel > ImageFromStream < TPixel > ( Stream stream )
22
23
{
23
- uint length = ( uint ) ( stream . Length - stream . Position ) ;
24
- using var nativeData = NSMutableData . FromLength ( ( int ) length ) ;
24
+ int length = ( int ) ( stream . Length - stream . Position ) ;
25
+ using var buffer = MemoryAllocator . Default . Allocate < byte > ( length ) ;
26
+ stream . ReadExactly ( buffer . Memory . Span ) ;
25
27
26
- var bytesSpan = new Span < byte > ( nativeData . MutableBytes . ToPointer ( ) , ( int ) length ) ;
27
- stream . ReadExactly ( bytesSpan ) ;
28
+ fixed ( byte * ptr = buffer . Memory . Span )
29
+ {
30
+ using var nativeData = NSData . FromBytesNoCopy ( ( IntPtr ) ptr , ( nuint ) length , false ) ;
31
+ using var nsImage = new NSImage ( nativeData ) ;
28
32
29
- using var nsImage = new NSImage ( nativeData ) ;
33
+ if ( nsImage . Handle == IntPtr . Zero )
34
+ throw new ArgumentException ( $ "{ nameof ( Image ) } could not be created from { nameof ( stream ) } .") ;
30
35
31
- if ( nsImage . Handle == IntPtr . Zero )
32
- throw new ArgumentException ( $ "{ nameof ( Image ) } could not be created from { nameof ( stream ) } .") ;
33
-
34
- var cgImage = nsImage . CGImage ;
35
- return ImageFromCGImage < TPixel > ( cgImage ) ;
36
+ var cgImage = nsImage . CGImage ;
37
+ return ImageFromCGImage < TPixel > ( cgImage ) ;
38
+ }
36
39
}
37
40
}
38
41
}
0 commit comments