简体   繁体   English

如何将NSImage转换为FireMonkey TBitmap?

[英]How to convert NSImage to FireMonkey TBitmap?

How to convert an NSImage to Delphis FireMonkey TBitmap? 如何将NSImage转换为Delphis FireMonkey TBitmap? The NSImage is being passed to me from an Objective C API. NSImage是从Objective C API传递给我的。 I am using Delphi XE8. 我正在使用Delphi XE8。

Try using NSImage.TIFFRepresentation data. 尝试使用NSImage.TIFFRepresentation数据。 Save it to memory stream and then load TBitmap from the stream. 将其保存到内存流,然后从该流中加载TBitmap。 Another way is using NSImage.CGImageForProposedRect, you may find a sample here: https://delphi-foundations.googlecode.com/svn/trunk/FMX%20Utilities/CCR.FMXClipboard.Mac.pas https://delphi-foundations.googlecode.com/svn/trunk/XE2%20book/13.%20Native%20APIs/Taking%20a%20screenshot/ScreenshotForm.pas 另一种方法是使用NSImage.CGImageForProposedRect,你可以在这里找到一个样本: https://delphi-foundations.googlecode.com/svn/trunk/FMX%20Utilities/CCR.FMXClipboard.Mac.pas https://开头德尔福基础。 googlecode.com/svn/trunk/XE2%20book/13.%20Native%20APIs/Taking%20a%20screenshot/ScreenshotForm.pas

Update: OK, here comes my solution: 更新:好的,这是我的解决方案:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM