iOS Question How to rotate bitmap

chjk

Member
Licensed User
Longtime User
This b4a code
B4X:
Sub RotateImage(original As Bitmap, degree As Float) As Bitmap
   Dim matrix As JavaObject
   matrix.InitializeNewInstance("android.graphics.Matrix", Null)
   matrix.RunMethod("postRotate", Array(degree))
   Dim bmp As JavaObject
   bmp.InitializeStatic("android.graphics.Bitmap")
   Dim NewImage As Bitmap = bmp.RunMethod("createBitmap", Array(original, 0, 0, original.Width, original.Height, _
     matrix, True))
   Return NewImage
End Sub

Note that the size of the BMP after rotation is changed accordingly.

I want to know how to write b4i code?
 

chjk

Member
Licensed User
Longtime User
This rotate bitmap b4i code:)

B4X:
Sub RotateImageByDegrees(original As Bitmap, degree As Float) As Bitmap
    original= nome.RunMethod("imageRotatedByDegrees::", Array(original,degree))
  Return original
End Sub

Sub RotateImageByRadians(original As Bitmap, radians As Float) As Bitmap
    original= nome.RunMethod("imageRotatedByRadians::", Array(original,radians))
  Return original
End Sub
#If OBJC
CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};
- (UIImage *)imageRotatedByRadians:(UIImage *)bmp:(CGFloat)radians
{
   return [self imageRotatedByDegrees:bmp:RadiansToDegrees(radians)];
}
- (UIImage *)imageRotatedByDegrees:(UIImage *)bmp:(CGFloat)degrees
{ 
   // calculate the size of the rotated view's containing box for our drawing space
   UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,bmp.size.width, bmp.size.height)];
   CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
   rotatedViewBox.transform = t;
   CGSize rotatedSize = rotatedViewBox.frame.size;
 
   // Create the bitmap context
   UIGraphicsBeginImageContext(rotatedSize);
   CGContextRef bitmap = UIGraphicsGetCurrentContext();
 
   // Move the origin to the middle of the image so we will rotate and scale around the center.
   CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
 
   // Rotate the image context
   CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
 
   // Now, draw the rotated/scaled image into the context
   CGContextScaleCTM(bitmap, 1.0, -1.0);
   CGContextDrawImage(bitmap, CGRectMake(-bmp.size.width / 2, -bmp.size.height / 2, bmp.size.width, bmp.size.height), bmp.CGImage);
 
   UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return newImage;
}
#End If
 
Upvote 0

jazzzzzzz

Active Member
Licensed User
Longtime User
This rotate bitmap b4i code:)

B4X:
Sub RotateImageByDegrees(original As Bitmap, degree As Float) As Bitmap
    original= nome.RunMethod("imageRotatedByDegrees::", Array(original,degree))
  Return original
End Sub

Sub RotateImageByRadians(original As Bitmap, radians As Float) As Bitmap
    original= nome.RunMethod("imageRotatedByRadians::", Array(original,radians))
  Return original
End Sub
#If OBJC
CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};
- (UIImage *)imageRotatedByRadians:(UIImage *)bmp:(CGFloat)radians
{
   return [self imageRotatedByDegrees:bmp:RadiansToDegrees(radians)];
}
- (UIImage *)imageRotatedByDegrees:(UIImage *)bmp:(CGFloat)degrees
{
   // calculate the size of the rotated view's containing box for our drawing space
   UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,bmp.size.width, bmp.size.height)];
   CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
   rotatedViewBox.transform = t;
   CGSize rotatedSize = rotatedViewBox.frame.size;

   // Create the bitmap context
   UIGraphicsBeginImageContext(rotatedSize);
   CGContextRef bitmap = UIGraphicsGetCurrentContext();

   // Move the origin to the middle of the image so we will rotate and scale around the center.
   CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

   // Rotate the image context
   CGContextRotateCTM(bitmap, DegreesToRadians(degrees));

   // Now, draw the rotated/scaled image into the context
   CGContextScaleCTM(bitmap, 1.0, -1.0);
   CGContextDrawImage(bitmap, CGRectMake(-bmp.size.width / 2, -bmp.size.height / 2, bmp.size.width, bmp.size.height), bmp.CGImage);

   UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return newImage;
}
#End If
perfect...!!!!!
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
the code on post 3 always give me a object was not initialized (nS object) error. What is not initialized at this code ?
 
Upvote 0
Top