Saving to Photos Album

The declare for UIImageWriteToSavedPhotosAlbum that is used in several examples in the Forum does not seem to be working.

The problem may be related to the fact that the Picture.handle is not returning a proper value, but I have tried using a memory block or an ios image.

Anyone have a solution?

-Bob

It would be helpful to see your code for the declare

You use CopyOSHandle with Picture.HandleType.iOSUIImage to get the UIImage?

Christian,

Tried that. No joy.

The declare (Did not write this myself):
Declare Sub UIImageWriteToSavedPhotosAlbum Lib “UIKit” _
(img As ptr, target As ptr, sel As ptr, info As ptr)

and how are you calling it?

@Bob_Gordon

Did you happen to enable the Photos Access capability in the iOS Advanced Tab?

I used code that looks like this:

Declare Sub UIImageWriteToSavedPhotosAlbum Lib "UIKit" (img As Ptr, target As Ptr, sel As Ptr, info As Ptr)
Var p As Ptr = pic.CopyOSHandle(picture.HandleType.iOSUIImage)
UIImageWriteToSavedPhotosAlbum(p, Nil, Nil, Nil)

and as long as I also have the Photos Access capability enabled, it works just fine. Otherwise it crashes the app (Apple Security)

Greg,

That must be it. I’m off to the Advanced tab.

Thanks.

Is there an easy way to get the result of calling this function?

Passing an iOSBlock as 3rd parameter doesn’t seem to work.

From looking at the documentation you will need to make a Xojo class that adds the selector as a member and then catch the call. An iOSBlock will not do what you need. In iOSKit you can look for the use of BuildTargetClass which is how you’ll have to do it.

https://developer.apple.com/documentation/uikit/1619125-uiimagewritetosavedphotosalbum?language=objc

2 Likes

If needed I could make a plugin class for this.

1 Like

Special thanks to Greg and Christian.

Here’s a method that works: (See below for comments.)

Public Sub SaveToPhotosAlbum(Extends pPict As Picture)
// IMPORTANT: Must turn on Photos Access in iOS Build Settings Advanced

Declare Sub UIImageWriteToSavedPhotosAlbum Lib “UIKit” _
(img As ptr, target As ptr, sel As ptr, info As ptr)

Var pt As Ptr

pt = pPict.CopyOSHandle(Picture.HandleType.iOSUIImage)
UIImageWriteToSavedPhotosAlbum(pt,Nil,Nil,Nil)

End Sub


Issues:

  1. In Xojo 2020 2.1 Picture.handle returns 000000.
  2. Somewhere in the docs about memory blocks it suggests that one can pass a memory block to a declare. You can, but no picture.
  3. It’s essential to turn on access to Photos in the Advanced tab. If not, boom.
  4. Finally, once I had things working, I discovered that the picture was blank. In my app, I had made the picture but didn’t draw its contents!

Hope this helps.

I also added a module to the MBS Xojo Plugins.
See iOSPictureMBS module.

4 Likes

Use CopyOSHandle instead.

Greg,

Thanks, but you’ll note in the code I posted, that that’s what I did.

-Bob

Strange. Where are you getting the picture from?

The picture is generated by the app. It’s a little drawing toy for kids.

Try saving it to disk and reloading it with Picture.Open

Hi Bob,
Did you get this working?
I tried to implement the method but compile error says UIKit doesn’t exist
Declare Sub UIImageWriteToSavedPhotosAlbum Lib “UIKit” (img As ptr, target As ptr, sel As ptr, info As ptr)

Martin,

I did, but the error message: Declares direct into the runtime via Lib “” are no longer allowed suggests something has changed. What version of Xojo are you using? My work was in an earlier version. I need to move the project to the latest version to get it ready for the App Store.

-Bob

You tried our iOSPictureMBS module?

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.