如何将NSImage转换为delphi FireMonkey TBitmap?NSImage是从Objective C API传递给我的。我使用的是Delphi XE8。
尝试使用NSImage。TIFFRepresentation数据。将其保存到内存流中,然后从流中加载TBitmap。另一种方法是使用NSImage。cgimageforproposdirect,你可以在这里找到一个示例:https://delphi-foundations.googlecode.com/svn/trunk/FMX%20Utilities/CCR.FMXClipboard.Mac.pashttps://delphi-foundations.googlecode.com/svn/trunk/XE2%20book/13.%20Native%20APIs/Taking%20a%20screenshot/ScreenshotForm.pas
更新:好的,这是我的解决方案:
function PutBytesCallback(info: Pointer; buffer: Pointer; count: Longword): Longword; cdecl;
begin
Result := TStream(info).Write(buffer^, count)
end;
procedure ReleaseConsumerCallback(info: Pointer); cdecl;
begin
end;
function NSImageToBitmap(Image: NSImage; Bitmap: TBitmap): Boolean;
var
LStream: TMemoryStream;
LCGImageRef: CGImageRef;
LCallbacks: CGDataConsumerCallbacks;
LConsumer: CGDataConsumerRef;
LImageDest: CGImageDestinationRef;
begin
Result := False;
LStream := TMemoryStream.Create;
LImageDest := nil;
LConsumer := nil;
try
LCallbacks.putBytes := PutBytesCallback;
LCallbacks.releaseConsumer := ReleaseConsumerCallback;
LCGImageRef := Image.CGImageForProposedRect(nil, nil, nil);
LConsumer := CGDataConsumerCreate(LStream, @LCallbacks);
if LConsumer <> nil then
LImageDest := CGImageDestinationCreateWithDataConsumer(LConsumer, CFSTR('public.png'), 1, nil);
if LImageDest <> nil then
begin
CGImageDestinationAddImage(LImageDest, LCGImageRef, nil);
CGImageDestinationFinalize(LImageDest);
LStream.Position := 0;
Bitmap.LoadFromStream(LStream);
Result := True
end
finally
if LImageDest <> nil then CFRelease(LImageDest);
if LConsumer <> nil then CGDataConsumerRelease(LConsumer);
LStream.Free
end;
end;