SkOSFile_ios.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright 2017 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkOSFile_ios_DEFINED
  8. #define SkOSFile_ios_DEFINED
  9. #include "include/core/SkString.h"
  10. #ifdef SK_BUILD_FOR_IOS
  11. #import <CoreFoundation/CoreFoundation.h>
  12. static bool ios_get_path_in_bundle(const char path[], SkString* result) {
  13. // Get a reference to the main bundle
  14. CFBundleRef mainBundle = CFBundleGetMainBundle();
  15. // Get a reference to the file's URL
  16. CFStringRef pathRef = CFStringCreateWithCString(nullptr, path, kCFStringEncodingUTF8);
  17. // We use "data" as our subdirectory to match {{bundle_resources_dir}}/data in GN
  18. // Unfortunately "resources" is not a valid top-level name in iOS, so we push it one level down
  19. CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, pathRef, nullptr, CFSTR("data"));
  20. CFRelease(pathRef);
  21. if (!imageURL) {
  22. return false;
  23. }
  24. if (!result) {
  25. return true;
  26. }
  27. // Convert the URL reference into a string reference
  28. CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle);
  29. CFRelease(imageURL);
  30. // Get the system encoding method
  31. CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
  32. // Convert the string reference into an SkString
  33. result->set(CFStringGetCStringPtr(imagePath, encodingMethod));
  34. CFRelease(imagePath);
  35. return true;
  36. }
  37. #endif
  38. #endif // SkOSFile_ios_DEFINED