iOS Question Crop image after select from camera roll

pedrocam

Member
Licensed User
Hi B4Xrs

After reading all of this plus more I made a working solution for myself. It is in no way beautiful or efficient, but it works, and that's what matters right now. I'm kind of new to B4i so realize that.

The code below will crop, and resize an image. I use it to create a square profile picture from the rectangular images on the phone. It uses iMedia to access the pictures.

I Use two ImageViews, one ScrollView, and a couple buttons.
Hope it helps:


B4X:
Sub CameraImageSelectButton_Click
    cam.Initialize("cam", Page1)
    cam.SelectFromPhotoLibrary(Sender, cam.TYPE_ALL)
End Sub

Sub Cam_Complete (Success As Boolean, Image As Bitmap, VideoPath As String)
    If Success Then
        If Image.IsInitialized Then
    ImageView1.Bitmap = Image
          
    ScrollView1.Panel.AddView(ImageView1,0,0,(ImageView1.Bitmap.Width * .3),(ImageView1.Bitmap.Height * .3)) 'im reducing the image size by 1/3 because it is huge
    ScrollView1.ContentHeight = ImageView1.Height
    ScrollView1.ContentWidth = ImageView1.Width
        Else
            Msgbox("Error retrieving image","Error")
        End If
    End If
End Sub


Sub BtnSave_Click
Dim cnv As Canvas

cnv.Initialize(ScrollView1)

cnv.Refresh
Dim rectDest As Rect
rectDest.Initialize(0, 0, ImageView2.Width, ImageView2.Height)

cnv.DrawView(ScrollView1,rectDest)

Dim bmp As Bitmap
bmp = cnv.CreateBitmap

cnv.Refresh
Dim out As OutputStream = File.OpenOutput(File.DirDocuments, "ProfPic.jpg", False)
bmp.WriteToStream(out, 100, "JPEG")
out.Close

ImageView2.Bitmap = LoadBitmap(File.DirDocuments, "ProfPic.jpg")

End Sub

Sub BtnEnlarge_Click
    ImageView1Test.Width = (ImageView1Test.Width * 1.5)
    ImageView1Test.Height = (ImageView1Test.Height * 1.5)
  
    ImgScroll.ContentHeight = ImageView1Test.Height
    ImgScroll.ContentWidth = ImageView1Test.Width
End Sub

Sub BtnSmaller_Click
        ImageView1Test.Width = (ImageView1Test.Width / 1.5)
    ImageView1Test.Height = (ImageView1Test.Height / 1.5)
  
    ImgScroll.ContentHeight = ImageView1Test.Height
    ImgScroll.ContentWidth = ImageView1Test.Width
End Sub
 

tamayo461

Member
Licensed User
Hi B4Xrs

After reading all of this plus more I made a working solution for myself. It is in no way beautiful or efficient, but it works, and that's what matters right now. I'm kind of new to B4i so realize that.

The code below will crop, and resize an image. I use it to create a square profile picture from the rectangular images on the phone. It uses iMedia to access the pictures.

I Use two ImageViews, one ScrollView, and a couple buttons.
Hope it helps:


B4X:
Sub CameraImageSelectButton_Click
    cam.Initialize("cam", Page1)
    cam.SelectFromPhotoLibrary(Sender, cam.TYPE_ALL)
End Sub

Sub Cam_Complete (Success As Boolean, Image As Bitmap, VideoPath As String)
    If Success Then
        If Image.IsInitialized Then
    ImageView1.Bitmap = Image
         
    ScrollView1.Panel.AddView(ImageView1,0,0,(ImageView1.Bitmap.Width * .3),(ImageView1.Bitmap.Height * .3)) 'im reducing the image size by 1/3 because it is huge
    ScrollView1.ContentHeight = ImageView1.Height
    ScrollView1.ContentWidth = ImageView1.Width
        Else
            Msgbox("Error retrieving image","Error")
        End If
    End If
End Sub


Sub BtnSave_Click
Dim cnv As Canvas

cnv.Initialize(ScrollView1)

cnv.Refresh
Dim rectDest As Rect
rectDest.Initialize(0, 0, ImageView2.Width, ImageView2.Height)

cnv.DrawView(ScrollView1,rectDest)

Dim bmp As Bitmap
bmp = cnv.CreateBitmap

cnv.Refresh
Dim out As OutputStream = File.OpenOutput(File.DirDocuments, "ProfPic.jpg", False)
bmp.WriteToStream(out, 100, "JPEG")
out.Close

ImageView2.Bitmap = LoadBitmap(File.DirDocuments, "ProfPic.jpg")

End Sub

Sub BtnEnlarge_Click
    ImageView1Test.Width = (ImageView1Test.Width * 1.5)
    ImageView1Test.Height = (ImageView1Test.Height * 1.5)
 
    ImgScroll.ContentHeight = ImageView1Test.Height
    ImgScroll.ContentWidth = ImageView1Test.Width
End Sub

Sub BtnSmaller_Click
        ImageView1Test.Width = (ImageView1Test.Width / 1.5)
    ImageView1Test.Height = (ImageView1Test.Height / 1.5)
 
    ImgScroll.ContentHeight = ImageView1Test.Height
    ImgScroll.ContentWidth = ImageView1Test.Width
End Sub
Thanks my frend...
 
Top