iOS Question Crop image after select from camera roll

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
Hi All.

I need to select a photo from camera roll, and crop the image in a square format, and after show the cropped image in a ImageView
 

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi.
I attachment example of crop image view gallery and show it and roundimageview
 

Attachments

  • Attachment.zip
    487.7 KB · Views: 327
Upvote 0

pedrocam

Member
Licensed User
Longtime 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
 
Upvote 0

tamayo461

Member
Licensed User
Longtime 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...
 
Upvote 0
Top