使用 IFMXImageManagerService 时 iOS 上的访问冲突

Access Violation on iOS when use IFMXImageManagerService

我正在使用这个简单的代码:

procedure TForm1.Button1Click(Sender: TObject);
var servicios: IFMXImageManagerService;
    i:integer;
begin
    i:=servicios.GetCount;
    showmessage(inttostr(i));
end;

我收到一条 iOS 消息:“地址 0000000104BB0460 发生访问冲突,访问地址 00000000000000000”。

我对 IFMXImageManagerService 的所有尝试都会触发违规消息。

拜托,有人知道为什么吗?

谢谢!

您没有初始化 servicios 以指向任何有意义的东西,因此当然调用任何方法,如 servicios.GetCount(),都会失败。

您需要使用TPlatformServices.GetPlatformService() or TPlatformServices.SupportsPlatformService()来初始化servicios。这在 Embarcadero 的文档中有解释:

FireMonkey Platform Services

To use a platform service, you must:

  • Add a reference to the unit where your service is declared, such as FMX.Platform, to your unit.
  • Call TPlatformServices.SupportsPlatformService with the target platform service as a parameter to determine whether or not the specified platform service is supported at run time.
  • If SupportsPlatformService returns True, use TPlatformServices.GetPlatformService to access the actual platform service, and cast the returned service appropriately. You can alternatively use SupportsPlatformService to obtain the service as well.

试试这个:

uses
  ..., FMX.Platform, FMX.MediaLibrary;

procedure TForm1.Button1Click(Sender: TObject);
var
  servicios: IFMXImageManagerService;
  i: integer;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXImageManagerService, IInterface(servicios)) then
  begin
    i := servicios.GetCount;
    ShowMessage(IntToStr(i));
  end else
    ShowMessage('Image Manager not supported');
end;