I Want to rotate UIImage in 90 degrees with this method :
static inline double radians (double degrees) {return degrees * M_PI/180;}
UIImage* rotate(UIImage* src, UIImageOrientation orientation)
{
UIGraphicsBeginImageContext(src.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orientation == UIImageOrientationRight) {
CGContextRotateCTM (context, radians(90));
} else if (orientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, radians(-90));
} else if (orientation == UIImageOrientationDown) {
// NOTHING
} else if (orientation == UIImageOrientationUp) {
CGContextRotateCTM (context, radians(90));
}
[src drawAtPoint:CGPointMake(0, 0)];
return UIGraphicsGetImageFromCurrentImageContext();
}
the problem is that it give me a blank UIImage
Here is the code :-
UIImage *PortraitImage;
PortraitImage = [[UIImage alloc] initWithCGImage: currentImage.CGImage
scale: 1.0
orientation: UIImageOrientationLeft];
if you want to rotate UIImage at some particular angle then you can use CIIFilter to rotate at whatever angle you want but that is possible in IOS 5.0
CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:#"old-country-rain.jpg"]];
CIFilter *RotateFilter = [CIFilter filterWithName:#"CIStraightenFilter"];
[RotateFilter setValue:[NSNumber numberWithFloat: 1.04667f] forKey:#"inputAngle"];
[RotateFilter setValue:inputImage forKey:kCIInputImageKey];
CIImage *displayImage = RotateFilter.outputImage;
UIImage *finalImage = [UIImage imageWithCIImage:displayImage];
Related
I want to rotate image (left and right rotation) with angle 90.I am using following code it rotate left with angle 360.how can i rotate my image with angle 90.
-(IBAction)btnLeftRotateClicked:(id)sender
{
CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:#"transform"];
rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 0.25f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1;
[imageAdjustView.layer addAnimation:rotationAnimation forKey:#"rotationAnimation"];
}
Thanks.
Why don't you use the affineTransform if you just want to rotate the image in 2D
CGAffineTransformMakeRotation(-M_PI * 0.5);
A rotation of 360 degree is no Rotation. Guess you mean 180 = PI. Thus, 90 degree = .5*PI.
If you want to use some easy drop in classes that rotate a UIImage and also scale it, try looking at: http://bit.ly/w3r5t6 it is a UIImage category called WBImage
#M.S.B:
I think this link will help you.
How to Rotate a UIImage 90 degrees?
How to programmatically rotate image by 90 Degrees in iPhone?
You can refer to this answer by fbrereto in the above link
What about something like:
static inline double radians (double degrees) {return degrees * M_PI/180;}
UIImage* rotate(UIImage* src, UIImageOrientation orientation)
{
UIGraphicsBeginImageContext(src.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orientation == UIImageOrientationRight) {
CGContextRotateCTM (context, radians(90));
} else if (orientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, radians(-90));
} else if (orientation == UIImageOrientationDown) {
// NOTHING
} else if (orientation == UIImageOrientationUp) {
CGContextRotateCTM (context, radians(90));
}
[src drawAtPoint:CGPointMake(0, 0)];
return UIGraphicsGetImageFromCurrentImageContext();
}
EDIT: You can refer to answer given by Sabobin under the link How to programmatically rotate image by 90 Degrees in iPhone?. I have posted the same code so that you can refer it from here:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"my_image.png"]];
//set point of rotation
myImageView.center = CGPointMake(100.0, 100.0);
//rotate rect
myImageView.transform = CGAffineTransformMakeRotation(3.14159265/2);
Hope this helps you.
I have a problem
When I get an image from iPhone camera. i can get that in all orientations Like LeftLandscape, RightLandscape, Portrait and portrait upside down.
Now how can I rotate the image fetched from Camera Delegates to only one orientation i.e. Only in Portrait OR Landscape Right
Can anybody HElp ?
Try this...
CGSize size = sizeOfImage;
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextRotateCTM(ctx, angleInRadians);
CGContextDrawImage(UIGraphicsGetCurrentContext(),
CGRectMake(0,0,size.width, size.height),
image);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
Also refer this link...it will help you out...
http://www.platinumball.net/blog/2009/03/30/iphone-uiimage-rotation-and-mirroring/
NYXImagesKit: Handy UIImage Categories
By Benjamin Godard
This is a handy set of categories on UIImage for rotating, resizing, filtering, enhancing and more, in an efficient and easy-to-use manner. Might be useful in some of your apps!
Try this.It will be helpful
-(UIImage *)resizeImage:(UIImage *)image {
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
int width, height;
width = 640;//[image size].width;
height = 640;//[image size].height;
CGContextRef bitmap;
if (image.imageOrientation == UIImageOrientationUp | image.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
}
if (image.imageOrientation == UIImageOrientationLeft) {
NSLog(#"image orientation left");
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -height);
} else if (image.imageOrientation == UIImageOrientationRight) {
NSLog(#"image orientation right");
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -width, 0);
} else if (image.imageOrientation == UIImageOrientationUp) {
NSLog(#"image orientation up");
} else if (image.imageOrientation == UIImageOrientationDown) {
NSLog(#"image orientation down");
CGContextTranslateCTM (bitmap, width,height);
CGContextRotateCTM (bitmap, radians(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
Im developing a photo app.
Here i'm using the images from both photo library and also the images taking through camera and displaying it in a image view.
The images from photo library is coming in correct orientation but camera images are displaying in different orientation.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"MyFolder"];
NSString *imagePath = [dataPath stringByAppendingPathComponent:#"girl.jpg"];
if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
UIImage *image = [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:imagePath].CGImage scale:bieberImage.image.scale
orientation:NO];
// bieberImage.transform = CGAffineTransformMakeRotation(1.57079633);
CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:#"contents"];
crossFade.duration = 3.0;
crossFade.fromValue = (id)bieberImage.image.CGImage;
crossFade.toValue = (id)image.CGImage;
[self.bieberImage.layer addAnimation:crossFade forKey:#"animateContents"];
}
Can anyone tell me how can i correct this problem.
Thanks in advance.
Replace:
UIImage *image = [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:imagePath].CGImage scale:bieberImage.image.scale
orientation:NO];
With:
UIImage *image = [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:imagePath].CGImage scale:bieberImage.image.scale
orientation:UIImageOrientationRight];
Edit:
Or try this:
- (UIImage*) rotateImage:(UIImage* )src {
UIImageOrientation orientation = src.imageOrientation;
UIGraphicsBeginImageContext(src.size);
[src drawAtPoint:CGPointMake(0, 0)];
CGContextRef context = UIGraphicsGetCurrentContext();
if (orientation == UIImageOrientationRight) {
CGContextRotateCTM (context, [self radians:90]);
} else if (orientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, [self radians:90]);
} else if (orientation == UIImageOrientationDown) {
// NOTHING
} else if (orientation == UIImageOrientationUp) {
CGContextRotateCTM (context, [self radians:0]);
}
return UIGraphicsGetImageFromCurrentImageContext();
}
- (CGFloat) radians:(int)degrees {
return (degrees/180)*(22/7);
}
-(UIImage *)update4:(UIImage *)image {
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
int width, height;
width = image.size.width;
height = image.size.height;
if(width>320)
{
width = 320;
height = height * 320/width;
}
if(height>480)
{
height = 480;
width = width * 480/height;
}
CGContextRef bitmap;
if (image.imageOrientation == UIImageOrientationUp | image.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
}
if (image.imageOrientation == UIImageOrientationLeft) {
//NSLog(#"image orientation left");
CGContextRotateCTM (bitmap, 3.14/180*90);
CGContextTranslateCTM (bitmap, 0, -height);
} else if (image.imageOrientation == UIImageOrientationRight) {
//NSLog(#"image orientation right");
CGContextRotateCTM (bitmap, -3.14/180*90);
CGContextTranslateCTM (bitmap, -width, 0);
} else if (image.imageOrientation == UIImageOrientationUp) {
//NSLog(#"image orientation up");
} else if (image.imageOrientation == UIImageOrientationDown) {
//NSLog(#"image orientation down");
CGContextTranslateCTM (bitmap, width,height);
CGContextRotateCTM (bitmap, -3.14/180*180);
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
UIImage *updateImage = [self update4:image];
}
I have an app that takes the photos via the camera or select it from the library and save it as jpg in the documents folder. But before saving it, I would like to rotate is by 90degrees.
Hope anyone can help me out here.
Thanks
For iOS 4 and later:
UIImage imageWithCGImage:scale:orientation:
For iOS 3 and previous:
How to Rotate a UIImage 90 degrees?
static inline double radians (double degrees) {return degrees * M_PI/180;}
UIImage* rotate(UIImage* src, UIImageOrientation orientation)
{
UIGraphicsBeginImageContext(src.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orientation == UIImageOrientationRight) {
CGContextRotateCTM (context, radians(90));
} else if (orientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, radians(-90));
} else if (orientation == UIImageOrientationDown) {
// NOTHING
} else if (orientation == UIImageOrientationUp) {
CGContextRotateCTM (context, radians(90));
}
[src drawAtPoint:CGPointMake(0, 0)];
return UIGraphicsGetImageFromCurrentImageContext();
}
You can do it like
static inline double radians (double degrees) {return degrees * M_PI/180;}
UIImage* rotate(UIImage* src, UIImageOrientation orientation)
{
UIGraphicsBeginImageContext(src.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orientation == UIImageOrientationRight) {
CGContextRotateCTM (context, radians(90));
} else if (orientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, radians(-90));
} else if (orientation == UIImageOrientationDown) {
// NOTHING
} else if (orientation == UIImageOrientationUp) {
CGContextRotateCTM (context, radians(90));
}
[src drawAtPoint:CGPointMake(0, 0)];
return UIGraphicsGetImageFromCurrentImageContext();
}
Taken from here
img.transform = CGAffineTransformMakeRotation(3.14159265/2);
I am working on a pixelate application for iPhone. Because the pictures taken with the iPhone 4 camera are too big and therefore the application is working really slow when updating the pixelated picture, I am trying to create tiles of the picture first and just update the tiles not the hole picture.
When building the tiles, it works for camera roll picture taken in landscape mode (2592 x 1936 pxl) and with low resolution pictures, but not with picture taken in portrait mode (1936 x 2592 pxl).
The code for cutting the tiles from the original image looks like this:
for (i = 0; i < NrOfTilesPerHeight; i++) {
for (j = 0; j < NrOfTilesPerWidth; j++) {
CGRect imageRect = CGRectMake(j*TILE_WIDTH, i*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
CGImageRef image = CGImageCreateWithImageInRect(aux.CGImage, imageRect);
UIImage *img = [UIImage imageWithCGImage:image];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
}
}
The problem is that the image created with these tiles it is rotated to a 90 degree angle anticlockwise.
Thank you a lot,
Andrei
It is because the imageOrientation isn't taken into account.
There is a similar question (Resizing UIimages pulled from the Camera also ROTATES the UIimage?) and I've modified the code a bit to work with cropping image.
Here you are:
static inline double radians (double degrees) {return degrees * M_PI/180;}
+(UIImage*)cropImage:(UIImage*)originalImage toRect:(CGRect)rect{
CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], rect);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
CGContextRef bitmap = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
if (originalImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -rect.size.height);
} else if (originalImage.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -rect.size.width, 0);
} else if (originalImage.imageOrientation == UIImageOrientationUp) {
// NOTHING
} else if (originalImage.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, rect.size.width, rect.size.height);
CGContextRotateCTM (bitmap, radians(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, rect.size.width, rect.size.height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *resultImage=[UIImage imageWithCGImage:ref];
CGImageRelease(imageRef);
CGContextRelease(bitmap);
CGImageRelease(ref);
return resultImage;
}