Platforms to show: All Mac Windows Linux Cross-Platform
/iOS/Photo Picker/PHPickerControl test
Last modified Sun, 18th Jan 2025.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /iOS/Photo Picker/PHPickerControl test
Download this example: PHPickerControl test.zip
Project "PHPickerControl test.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Opening()
End EventHandler
EventHandler Function UnhandledException(error As RuntimeException) As Boolean
For Each s As String In error.Stack
System.DebugLog s
Next
End EventHandler
End Class
Class PHPickerControlWindow Inherits DesktopWindow
Control PHPickerControlMBS1 Inherits DesktopPHPickerControlMBS
ControlInstance PHPickerControlMBS1 Inherits DesktopPHPickerControlMBS
EventHandler Sub BoundsChanged()
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub Closing()
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Function Configure() As PHPickerConfigurationMBS
System.DebugLog CurrentMethodName
configuration = New PHPickerConfigurationMBS
configuration.Filter = PHPickerFilterMBS.imagesFilter
// set to zero for unlimited
configuration.SelectionLimit = 1
configuration.Selection = configuration.SelectionDefault
configuration.PreferredAssetRepresentationMode = configuration.AssetRepresentationModeCompatible
Return configuration
End EventHandler
EventHandler Sub FrameChanged()
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub Opening()
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub didCloseContextualMenu(menu as NSMenuMBS, NSEvent as NSEventMBS)
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub didFinishPicking(Result() as PHPickerResultMBS)
System.DebugLog CurrentMethodName
Self.PickerFinished result
End EventHandler
EventHandler Sub viewDidAppear()
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Sub willShowContextualMenu(menu as NSMenuMBS, NSEvent as NSEventMBS)
System.DebugLog CurrentMethodName
End EventHandler
End Control
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
Var x As Integer = 0
For i As Integer = pictures.LastIndex DownTo 0
Var pic As Picture = Pictures(i)
Var faktor As Double = Min( g.Height / Pic.Height, g.Width / Pic.Width)
// Calculate new size
var w As Integer = Pic.Width * faktor
var h As Integer = Pic.Height * faktor
// draw picture in the new size
g.DrawPicture Pic, x, 0, w, h, 0, 0, Pic.Width, Pic.Height
x = x + w + 5
if x > g.Width then return // skip the ones outside the view
Next
End EventHandler
End Control
Sub LoadFileRepresentationCompleted(File as FolderItem, error as NSErrorMBS, result as PHPickerResultMBS)
System.DebugLog CurrentMethodName
if error <> nil then
MessageBox error.LocalizedDescription
end if
if file <> nil then
System.DebugLog file.NativePath
// normal opening
#if MBS.HasMacCIPlugin = false then
// load directly. Picture may be rotated
Var pic As Picture = Picture.Open(file)
#Else
// use CIImage to rotate to always be top left orientation
var ciImage As CIImageMBS = New CIImageMBS(file)
var properties As Dictionary = ciImage.properties
If properties.HasKey("Orientation") Then
var orientation As Integer = ciImage.properties.Value("Orientation")
ciImage = ciImage.imageByApplyingOrientation(orientation)
End If
Var cgPic As CGImageMBS = ciImage.CreateCGImage
Var pic As Picture = cgPic.Picture
#endif
Pictures.Add pic
Canvas1.Refresh
end if
End Sub
Sub PickerFinished(results() as PHPickerResultMBS)
System.DebugLog CurrentMethodName
self.Results = results
if results.Count = 0 then Return // cancel pressed?
var result as PHPickerResultMBS = results(0)
var registeredTypeIdentifiers() As String = result.TypeIdentifiers
System.DebugLog "Types: "+String.FromArray(registeredTypeIdentifiers, EndOfLine)
// HEIC available?
if registeredTypeIdentifiers.IndexOf("public.heic") >= 0 then
result.loadFileRepresentationForTypeIdentifier("public.heic", WeakAddressOf LoadFileRepresentationCompleted)
elseif registeredTypeIdentifiers.IndexOf("public.png") >= 0 then
// try png
const identifier = "public.png"
result.loadFileRepresentationForTypeIdentifier(identifier, WeakAddressOf LoadFileRepresentationCompleted)
else
// try jpeg
const identifier = "public.jpeg"
result.loadFileRepresentationForTypeIdentifier(identifier, WeakAddressOf LoadFileRepresentationCompleted)
end if
End Sub
Property Private Pictures() As Picture
Property Results() As PHPickerResultMBS
Property configuration As PHPickerConfigurationMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
End Project
See also:
Download this example: PHPickerControl test.zip
The items on this page are in the following plugins: MBS iOS Plugin.