iOS Question Image Resize & Crop question


Well-Known Member
Licensed User
Longtime User
Hi Guys

I have the following requirements:
1. Use the camera to take a picture
2. Display the picture in an image view with the proper scale (meaning without looking stretched)
3. Crop the picture and display on a white backdground equivalent to the original picture size
4. Upload the picture to the server

After the image is captured by the camera, I am calling ResizeImage, then I am displaying the image inside the ImageView, and results are as expected (meaning no stretching )

for B4A I am using

Sub ResizeImage(original As Bitmap, TargetX As Int, TargetY As Int) As Bitmap
    Dim origRatio As Float = original.Width / original.Height
    Dim targetRatio As Float = TargetX / TargetY
    Dim scale As Float
    If targetRatio > origRatio Then
        scale = TargetY / original.Height
        scale = TargetX / original.Width
    End If
    Dim c As Canvas
    Dim b As Bitmap
    b.InitializeMutable(TargetX, TargetY)
    'set the background
    Dim r As Rect
    Dim w = original.Width * scale, h = original.Height * scale As Int
    r.Initialize(TargetX / 2 - w / 2, TargetY / 2 - h / 2, TargetX / 2 + w / 2, TargetY / 2+ h / 2)
    c.DrawBitmap(original, Null, r)

    Log(b.Width & " " & b.Height)
    Return b
End Sub

For B4I

Sub ResizeBitmap(bmp As Bitmap, TargetX As Float, TargetY As Float) As Bitmap
   Dim img As ImageView
   Dim scaleX As Float = bmp.Width/ TargetX
   Dim scaleY As Float = bmp.Height/ TargetY
   img.Width = bmp.Width * scaleX
   img.Height = bmp.Height * scaleY
   Dim cvs As Canvas
   cvs.DrawBitmap(bmp, cvs.TargetRect)
   Dim res As Bitmap = cvs.CreateBitmap
   Return res
End Sub

The interesting thing is, later on why I use
 Dim bmp3 As Bitmap = ImageView1.Bitmap

and then if I try to find the ratio of bmp against imageView1, for B4A it is 1, for B4I it is 3.
Can some one please clarify, why B4I doesn't report 1, and what is the proper way of doing this

Next, can someone help with the correct syntax for the equivalent B4I code for the croping

'This one returns a white image with the selected section on top
Private Sub GetCroppedImage2()

    Private rectSource, rectDest As Rect
    Private cLeft, cTop As Int
    Dim rectImageWidth As Int
    Dim rectImageHeight As Int
    rectImageWidth = rectImage.Right - rectImage.Left
    rectImageHeight = rectImage.Bottom - rectImage.Top
'    imvCroped.Height = imvCroped.Width / rectImageWidth * rectImageHeight
#if B4A   
    imvCroped.Gravity = Gravity.FILL
    imvCroped.ContentMode = imvCroped.MODE_FILL
#end If   
    Private cvsCroped As Canvas
    cLeft = (rectImage.Left - imvImage.Left) * PixelScaleX
    cTop = (rectImage.Top - imvImage.Top) * PixelScaleY
    rectSource.Initialize(cLeft, cTop, cLeft + rectImageWidth * PixelScaleX, cTop + rectImageHeight * PixelScaleY)
    rectDest.Initialize(cLeft, cTop, cLeft + rectImageWidth * PixelScaleX, cTop + rectImageHeight * PixelScaleY)

#if B4A
    bmpCroped.InitializeMutable(bmpImage.Width, bmpImage.Height)

    cvsCroped.DrawBitmap(bmpImage, rectSource, rectDest)
    imvCroped.Bitmap = bmpCroped
   I am looking for the equivalent code to fill in here
#end if
End Sub


B4X founder
Staff member
Licensed User
Longtime User
The screen size (width and height) reported in iOS / B4i is normalized (scale = 1). It is similar to using 'dip' units in B4A.

You can find the true scale with GetDeviceLayoutValues.NonnormalizedScale. I guess that this is the source for the difference.

This sub creates a canvas that with a specific size:
Public Sub CreateCanvas (width1 As Int, height1 As Int) As Canvas
   Dim iv As ImageView
   iv.SetLayoutAnimated(0, 1, 0, 0, width1, height1)
   Dim c As Canvas
   Return c
End Sub
It is similar to B4A canvas with mutable bitmap.
Upvote 0