【问题标题】:How to convert NSImage to FireMonkey TBitmap?如何将 NSImage 转换为 FireMonkey TBitmap?
【发布时间】:2015-10-13 02:02:04
【问题描述】:

如何将 NSImage 转换为 Delphis FireMonkey TBitmap? NSImage 是从 Objective C API 传递给我的。我正在使用 Delphi XE8。

【问题讨论】:

  • 这个链接看起来很有希望stackoverflow.com/questions/3038820/…
  • 谢谢,但我不想将图像保存到文件中,我需要将从 Objective C API 接收到的 NSImage 对象实例转换为 Delphi FireMonkey TBitmap 实例...

标签: delphi firemonkey nsimage tbitmap


【解决方案1】:

尝试使用 NSImage.TIFFRepresentation 数据。将其保存到内存流,然后从流中加载 TBitmap。另一种方法是使用 NSImage.CGImageForProposedRect,你可以在这里找到一个示例: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

更新:好的,我的解决方案来了:

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;

【讨论】:

  • 我会试试的,不过,我假设你说的是 NSImage 中的表示数组属性...
  • 我尝试使用 NSImageRep 但失败了。在更新的答案中找到解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多