I'm developing an iOS 6 app for iPad. I've developed some code which rotates a UIImage. It works great with square images, but when the images aren't square they get cropped, so you only see a part of the image (a square).
My code:
UIGraphicsBeginImageContext(image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM( context, 0.5f * image.size.width, 0.5f * image.size.height ) ;
CGContextRotateCTM( context, -1.5707963267949) ;
[image drawInRect:(CGRect){ { -imatgetemporal.size.width * 0.5f, -image.size.height * 0.5f }, image.size }];
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
I think the problem is in the 0.5f, but I don't know how to solve it.
What can I do?
You need to make your graphics context big enough for the rotated image. You're using image.size, but should be using a the rotated size as the argument to UIGraphicsBeginImageContext. Additionally, your argument to drawInRect is incorrect. You need to offset the (rotated) origin x, which determines the y origin of the final image, in the following way: "Up" by half the original height (which puts the final image entirely off the top of the drawing context) then "down" by the full original width, which is the final image height.
const CGSize imageSize = image.size;
UIGraphicsBeginImageContext(CGSizeMake(imageSize.height, imageSize.width));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.5f * imageSize.width, 0.5f * imageSize.height ) ;
CGContextRotateCTM(context, -1.5707963267949) ;
[image drawInRect:(CGRect){ { imageSize.height * 0.5 - imageSize.width, -imageSize.width * 0.5f }, imageSize }];
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
Related
I'm facing the following problem : I have to merge two images bottomImg and topImg to create a new image C as a result of the merging.
I already know how to merge two images but in this case my goal is a little bit different.
top image may be zoomed and rotated.
I have follow this answer. But in this solution top image is cut while i rotate. simulator
actual stored image is document like
I have try this code
_parentViewGIf for bottomImg and for _ivGIF for topImg
- (UIImage *)mergeImage:(UIImage *)bottomImg withImage:(UIImage *)topImg {
float width=bottomImg.size.width*_ivGIF.frame.size.width/_parentViewGIf.frame.size.width;
float height=bottomImg.size.width*_ivGIF.frame.size.height/_parentViewGIf.frame.size.width;
UIImage * scaledTopImg = [self imageByScalingProportionallyWithImage:topImg ToSize:CGSizeMake(bottomImg.size.width, bottomImg.size.height)];
UIGraphicsBeginImageContext(scaledTopImg.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, scaledTopImg.size.height * 0.5, scaledTopImg.size.height * 0.5);
CGFloat angle = atan2(_ivGIF.transform.a, _ivGIF.transform.b);
CGContextRotateCTM(ctx, angle);
[scaledTopImg drawInRect:CGRectMake(- scaledTopImg.size.width * 0.5f, -(scaledTopImg.size.height * 0.5f), scaledTopImg.size.width , scaledTopImg.size.height )];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
float x=bottomImg.size.width*_ivGIF.frame.origin.x/_parentViewGIf.frame.size.width;
float y=bottomImg.size.width*_ivGIF.frame.origin.y/_parentViewGIf.frame.size.width;
UIGraphicsBeginImageContext(bottomImg.size);
[bottomImg drawInRect:CGRectMake(0, 0, bottomImg.size.width, bottomImg.size.height)];
[newImage drawInRect:CGRectMake(0, 0, newImage.size.width, newImage.size.height)];
UIImage *newImage2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage2;
}
Thanks in advance for any help or suggestion
I'm having some trouble with image rotation in iOS. I'm performing some image manipulation in the background of an app... I would like to rotate and crop the images. Currently, the rotation seems to be working correctly, but no matter what I have tried, the crop is off. I have performed these same operations in GIMP and found that the images crop correctly, so I believe it has something to do with the Quartz coordinate system. Furthermore, the greater the radians in either direction, the further "off" the crop becomes. Here is the code I am using to rotate and crop:
+(UIImage*)imageWithImageFile:(NSString*)imageFile
radians:(float)radians
imageSize:(CGSize)imageSize
croppedToRect:(CGRect)croppedToRect
{
UIImage* returnImg = nil;
#autoreleasepool {
CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([imageFile UTF8String]);
CGImageRef image = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, YES, kCGRenderingIntentDefault);
UIGraphicsBeginImageContext(croppedToRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(context, YES);
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextTranslateCTM(context, imageSize.width * 0.5,
imageSize.height * 0.5);
CGContextRotateCTM(context, radians);
CGContextTranslateCTM(context, -imageSize.width * 0.5, -imageSize.height * 0.5);
//Translate and scale upside-down to compensate for Quartz's inverted coordinate system
CGContextTranslateCTM(context, 0.0, imageSize.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, (CGRect) {croppedToRect.origin, imageSize}, image);
returnImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGDataProviderRelease(dataProvider);
CGImageRelease(image);
}
return returnImg;
}
I believe this is the solution: Since the context is a different size and has a different origin inside the original image, you must offset the point where you "paint" the image on the context (canvas for those of you more visual people). You first find the origin of the centered context, then compare it to the origin of the desired crop. The difference (or offset) can be subtracted from the center point of the context, which will offset the point at which the image is painted.
CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([imageFile UTF8String]);
CGImageRef image = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, YES, kCGRenderingIntentDefault);
UIGraphicsBeginImageContext(croppedToRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint contextCenter = CGPointMake(croppedToRect.size.width * 0.5f,
croppedToRect.size.height * 0.5f);
CGPoint centerOfImage = CGPointMake(imageSize.width * 0.5f,
imageSize.height * 0.5f);
CGPoint contextOriginInImage = CGPointMake(centerOfImage.x - contextCenter.x,
centerOfImage.y - contextCenter.y);
CGPoint desiredOrigin = croppedToRect.origin;
CGPoint offset = CGPointMake(desiredOrigin.x - contextOriginInImage.x,
desiredOrigin.y - contextOriginInImage.y);
CGContextTranslateCTM(context, contextCenter.x - offset.x, contextCenter.y - offset.y);
CGContextRotateCTM(context, radians);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextDrawImage(context, CGRectMake(-imageSize.width * 0.5f,
-imageSize.height * 0.5f,
imageSize.width,
imageSize.height), image);
returnImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGDataProviderRelease(dataProvider);
CGImageRelease(image);
Help, Im new to ios programming, I want to rotate may UIImage but I dont want the edges to be cut or loose some part of the image.
this is my code:
double angle = M_PI * 10/ 180; CGSize s = {image.size.width, image.size.height}; UIGraphicsBeginImageContext(s); CGContextRef ctx = UIGraphicsGetCurrentContext();
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, image.size.width/2, image.size.height/2);
transform = CGAffineTransformRotate(transform, angle);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(ctx, transform);
CGContextDrawImage(ctx,CGRectMake(-[image size].width/2,-[image size].height/2,image.size.width, image.size.height),image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
The image is rotating but the size of the frame does not change because of that some of the image has been cut.
OUTPUT:https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpf1/t1.0-9/1555286_761679470521535_1800180000235265553_n.jpg
EXPECTED OUTPUT: https://fbcdn-photos-g-a.akamaihd.net/hphotos-ak-xfa1/t1.0-0/10314770_761675840521898_6536715783383115855_a.jpg
Please help me thank you.
This might be what you are searching for.
Just copy the following code at the end of the .m file (after the #end) in which you want to rotate an image.
#interface UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
#end
#implementation UIImage (RotationMethods)
static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.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(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Then rotate your image like in the example below:
CGFloat degrees = 90;
yourImage = [yourImage imageRotatedByDegrees:degrees];
I have read numerous answers related to this, but I still can't get it to work.
I have a view where a user can sign their name. Here's how it looks: http://d.pr/i/McuE
I can successfully retrieve this image and save it to the file system, but I need to rotate it 90 degrees before saving so the signature reads from left-to-right.
// Grab the image
UIGraphicsBeginImageContext(self.signArea.bounds.size);
[self.signArea drawRect: self.signArea.bounds];
UIImage *signatureImage = UIGraphicsGetImageFromCurrentImageContext();
//-- ? -- Rotate the image (this doesn't work) -- ? --
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM(context, M_PI_2);
UIGraphicsEndImageContext();
//Save the image as a PNG in Documents folder
[UIImagePNGRepresentation(signatureImage) writeToFile:[[PPHelpers documentsPath] stringByAppendingPathComponent:#"signature-temp.png"] atomically:YES];
How can I rotate the image prior to saving?
Thanks in advance for your help. I'm on Xcode 5 using the iOS 7 SDK.
I finally got this to work using one of the answers on this post: How to Rotate a UIImage 90 degrees?
I used this method:
- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{
//Calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
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, (degrees * M_PI / 180));
//Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
And then called it like this:
//Rotate it
UIImage *rotatedImage = [self imageRotatedByDegrees:signatureImage deg:90];
Thanks, everyone.
I am implementing a zooming feature in a camera app using AVFoundation. I am scaling my preview view like this:
[videoPreviewView setTransform:CGAffineTransformMakeScale(cameraZoom, cameraZoom)];
Now, after I take a picture, I would like to zoom/crop the picture with the cameraZoom value before I save it to the camera roll. How best should I do this?
Edit: Using Justin's answer:
CGRect imageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], imageRect);
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef), CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), CGImageGetColorSpace(imageRef), CGImageGetBitmapInfo(imageRef));
CGContextScaleCTM(bitmapContext, scale, scale);
CGContextDrawImage(bitmapContext, imageRect, imageRef);
CGImageRef zoomedCGImage = CGBitmapContextCreateImage(bitmapContext);
UIImage* zoomedImage = [[UIImage alloc] initWithCGImage:imageRef];
It is zooming the image, but it is not taking the center of it, but rather seems to be taking the top right area. (I'm not positive).
The other problem, (I should have been clearer in the OP), is that the image remains the same resolution, but I would rather just crop it down.
+ (UIImage*)croppedImageWithImage:(UIImage *)image zoom:(CGFloat)zoom
{
CGFloat zoomReciprocal = 1.0f / zoom;
CGPoint offset = CGPointMake(image.size.width * ((1.0f - zoomReciprocal) / 2.0f), image.size.height * ((1.0f - zoomReciprocal) / 2.0f));
CGRect croppedRect = CGRectMake(offset.x, offset.y, image.size.width * zoomReciprocal, image.size.height * zoomReciprocal);
CGImageRef croppedImageRef = CGImageCreateWithImageInRect([image CGImage], croppedRect);
UIImage* croppedImage = [[UIImage alloc] initWithCGImage:croppedImageRef scale:[image scale] orientation:[image imageOrientation]];
CGImageRelease(croppedImageRef);
return croppedImage;
}
to scale/zoom:
create a CGBitmapContext
alter the context's transform (CGContextScaleCTM)
draw the image (CGContextDrawImage) - the rect you pass may be used to offset the origin and/or dimensions.
generate a new CGImage from the context (CGBitmapContextCreateImage)
to crop:
create a CGBitmapContext. pass NULL so the context creates is buffer for the bitmap.
draw the image as-is (CGContextDrawImage)
create a CGBitmapContext for the crop (with the new dimensions), using an offset of the first context's pixel data for the buffer.
generate a new CGImage from the second context (CGBitmapContextCreateImage)