B4i Library [CLASS] Photo management library based on PHAsset

This class is a collaborative effort between myself and Narek Adonts.

The basic architecture and original Objective C was provided by Narek, I did some additional Objective C (and I don't do Objective C) and a major tidy up and rationalization.

The enclosing example wrapper is intentionally minimalistic - it is linear in nature and requires you to analyse the log as you work through it.

The functionality covered is:
  • Initialize
  • PermissionGet
  • SmartAlbumsGet - get all smart albums and contents
  • UserAlbumsGet - get all user albums and contents
  • UserAlbumAdd - add a user album by its name
  • UserAlbumExists - check if a user album exists by its name
  • UserAlbumDelete - delete a user album by its name
  • PhotoAdd - add a photo as bitmap to a user album by its name - returns saved photos GUID
  • PhotoGet - get a photo as bitmap by its GUID
  • VideoAdd - add a video as ??? to a user album by its name - returns saved videos GUID
  • VideoGet - get a video as ??? by its GUID
  • AssetExists - check if photo or video exists by its GUID
  • AssetDelete - delete a photo or video by its GUID
The functionality not covered includes:
  • Cloud
  • Thumbnails
  • Add/delete pointer to a photo/video asset in a user album
I have been using this for some months and feel that it is stable, however...

Happy coding...

VERY IMPORTANT INSTALLATION NOTE

Because of the ridiculously small limit on size of files that can be uploaded, the attached PHAssets.zip file contains a dummy testvideo.mp4 in the Files folder of the project.

Find an .mp4 video of your choice and copy it to the Files folder with the name "testvideo.mp4"
 

Attachments

  • PHAssets.zip
    69.2 KB · Views: 25
Last edited:

JackKirk

Well-Known Member
Licensed User
Longtime User
Just going over some notes to myself on this:

If you look at the code under PhotoAdd and VideoAdd you will see I use a fairly primitive method to ascertain what is the GUID of the just added photo or video - is there a smarter way to do this?

Any advice welcome...
 

ilan

Expert
Licensed User
Longtime User
hi jack,

i am trying to load several images at once using phasset but i cant figure out how to do it.
i also noticed that the image picker is not showing each time i click the button. is there a way to show on every button click a UNSELECTED image picker where i can select 1 or more images and import them to my project? maybe a list of bmp's?

thanks, ilan
 

JackKirk

Well-Known Member
Licensed User
Longtime User
I assume you have installed the PHAssets.zip and added an example *.mp4 as per the first post.

As I had not looked at this for some time I downloaded as per first post and ran it in debug mode - works as exactly as advertised on my iPhone 7 running iOS 13.5.1 - what is your iPhone and iOS?

i am trying to load several images at once using phasset but i cant figure out how to do it.
Look at lines 187-203 - you have to add photos 1 at a time to your album

i also noticed that the image picker is not showing each time i click the button. is there a way to show on every button click a UNSELECTED image picker where i can select 1 or more images and import them to my project? maybe a list of bmp's?
This does not make sense to me in terms of the PHAssets.zip code - there is no "image picker" that I am aware of - maybe our terminology is different.

You should really look at the wrapper code very carefully, watch the log as the code executes. The wrapper is super simple and linear and explores 1 function at a time.
 

ilan

Expert
Licensed User
Longtime User
his does not make sense to me in terms of the PHAssets.zip code - there is no "image picker" that I am aware of - maybe our terminology is different.

thank you for your reply, i guess i misunderstood the use of this class.
i was looking for a lib where i can pick 1 or more images from the Photo Album of my ios device and use it in my app like showing them in imageviews the same as it is working in the iMedia library: https://www.b4x.com/android/forum/threads/imedia-library-camera-and-videoview.46144/#content

where you can chose an image from the photo album with this method: cam.SelectFromPhotoLibrary and it returns a bitmap that i can use in my app like putting inside an imageview. but the problem is that it is limited to only 1 image at once and i wanted to load more then 1 image at once.

anyway thank you for your support.
 

JackKirk

Well-Known Member
Licensed User
Longtime User
ilan,

You can do all of this - see the 3 minute video at:

http://treetopssmartphoto.com/operator.html

which demonstrates my projects customer app usage.

From about 1:45 onwards in the video I am doing precisely what you are talking about (I think :):):)).

The video was made using the Android version of the app but the iOS version of the app does exactly the same thing - using this PHAssets class.
 
Last edited:

ilan

Expert
Licensed User
Longtime User
From about 1:45 onwards in the video I am doing precisely what you are talking about (I think :):):)).

hi Jack,

i look at your video and i also tried to analyze your class yesterday for 3 hours. i could figure out how to load images from albums the only thing that i could not figure out is how can i open the dialog where i can see all images in all albums and select images so i can import them. it works only when i initialize the class and call smartalbumget for the first time then i get a message box where i can choose between KEEP CURRENT SELECTION or SELECT MORE.

what i want to archive is User clicks on a button > photo picker dialog appears > user choose photos and i go through his selection and import the assets.
i know how to import the assets but how can i open again the photo picker and reselect photos from the albums. for some reason i can do it only if the app starts but after calling again getalbums the photo picker is not showing anymore and i only get the already selected photos.

what do you do if you want to reselect again photos?
 

ilan

Expert
Licensed User
Longtime User
here is a short video that can maybe help to understand me better. as you can see in the video after the app start the first time and i click on a button and call this code:

B4X:
Dim ph As PHAssets
                ph.Initialize(Me, "Event_ph")
                ph.PermissionGet
                Wait For Event_ph_PermissionGet_done
                Log("Permission: " & ph.PermissionGet_result)
                If Not(ph.PermissionGet_result) Then Return
           
                LogColor("Following lists all 'smart' albums - a 'smart'" & CRLF & _
             "album is one which is created by iOS that you" & CRLF & _
             "can not create/delete either via the Photos" & CRLF & _
             "app or code such as this", Colors.Red)
             
                ph.SmartAlbumsGet(ph.PHAssetCollectionSubtypeSmartAlbumRecentlyAdded)
                Wait For Event_ph_SmartAlbumsGet_done
   
                If ph.SmartAlbumsGet_result.IsInitialized Then
                    Private wrk_phalbum As PHAlbum
                    wrk_phalbum.Initialize
                   
                    For wrk_i = 0 To ph.SmartAlbumsGet_result.Size -1
                        wrk_phalbum = ph.SmartAlbumsGet_result.Get(wrk_i)
                        Log("Smart Album: " & wrk_phalbum.Name & " " & wrk_phalbum.PHAssetsList.Size)
                        Log("list size: " & wrk_phalbum.PHAssetsList.Size)
                        For i = 0 To  wrk_phalbum.PHAssetsList.Size -1
                            Private wrk_phasset As PHAsset = wrk_phalbum.PHAssetsList.Get(i)
                            ph.PhotoGet( wrk_phasset.GUID)
                            wait for Event_ph_PhotoGet_done
                            If  ph.PhotoGet_result.IsInitialized Then mainlogo.Bitmap =  ph.PhotoGet_result
                        Next
                    Next
                End If

a picker dialog is shown and i can select photos from my albums. exiting the picker will not return me the photos only after calling again the same code i am getting the photos. but the dialog is not shown again and i cannot reselect photos again.

i understand that what shows the picker is the permission request ( ph.PermissionGet ) and then when i call it again and it returns true the picker is not show but i get all selected photos i selected at the first time the picker was shown. now it would make more sense to load the picker select images and return them in a list and always be able to select new images from the albums.


 

JackKirk

Well-Known Member
Licensed User
Longtime User
ilan,

Sorry for the delay in responding - I suspect we are on opposite sides of the blue marble.

Would you be adverse to sending me your entire project - you could do it by PM if you like.

I think that would give me a much better base for helping you.
 

JackKirk

Well-Known Member
Licensed User
Longtime User
ilan,

Did you fix your problem?
 

ilan

Expert
Licensed User
Longtime User
Top