Is there a way to create a UIImage without orientation? - iphone

I am about to crop images but facing the orientation issue while creating image using CGImageCreateWithImageInRect.
CGImageCreateWithImageInRect crops the image based on UIImage orientation so I cannot get the right images as I want.
I want the plain image from the UIImage/maybe-camera-image without orientation meta data.
Is there a way to achieve this?
EDIT:
I have the some selected rect of the 'UIImage'. If I apply the crop on the 'UIImage' it gives different output with some other orientation

Try this:
CGImageRef imageRef = CGImageCreateWithImageInRect(src.CGImage, croppingRect);
UIImage *result = [UIImage imageWithCGImage:imageRef scale:1.0f orientation:src.imageOrientation];

Could help someone!
-(UIImage*) removeImageOrientation:(UIImage*)ImgtakenByUser
{
UIGraphicsBeginImageContext(ImgtakenByUser.size);
[ImgtakenByUser drawInRect:CGRectMake(0, 0, ImgtakenByUser.size.width, ImgtakenByUser.size.height)];
UIImage *_image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return _image;
}
Thanks!

Related

Saving Image from UIImageView after manipulating Gestures

I'm trying to save the image after manipulating it with move, pinch and rotate gestoures.
All I get is the real image no matter what I tried,
I tried with UIGraphicsBeginImageContextWithOptions on the UIView container and on the UIIMageView without success.
Hope someone can help on this one.
Thanks,
Itay
You could try with this:
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
CGContextConcatCTM(UIGraphicsGetCurrentContext(), imageView.transform);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This is assuming your imageView is transformed by means of a CGAffineTransform applied to it.

How to create a new image from an existing image and a string over it

I need to create something like:
http://t3.gstatic.com/images?q=tbn:ANd9GcQZu06WspVUaFTn-C-YD6Pzu_oWH5NLA1Q8nPUZEdemllFIAMNn
So I need to place a user input string over an image in a specified spot, then save it in the memory.
How can I do it?
Create a new bitmap image context. See this for help creating-and-drawing-on-a-new-uiimage
Draw your UIImage in drawInRect
Create black, rounded and transparent image. See this for help:
resize-a-uiimage-the-right-way
Draw black image drawInRect
Draw your NSString text. see this for help how-to-display-text-using-quartz-on-the-iphone drawInRect or drawInPoint
Here is a small code example for cropping an image.
- (UIImage *)croppedImageWithRect:(CGRect)bounds {
CGImageRef imageRef = CGImageCreateWithImageInRect(scaleImage.CGImage, bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
Enjoy.
use a UItextView over an image to show such string.

how to crop image in to pieces using - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

i need to crop single image into 9 pieces.
for that most of them suggest the fallowing method.
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
But i did n't fine any where how do it using this.
can any one please help me.
how can i crop image using this method.
Thank u in advance.
The method stretchableImageWithLeftCapWidth:topCapHeight is not cropping the image. It allows you to stretch an image with vertical and horyzontal margin.
In the doc you will read :
stretchableImageWithLeftCapWidth:topCapHeight:
Creates and returns a new image object
with the specified cap values.
To crop an image you should use CGImageCreateWithImageInRect :
CGImageCreateWithImageInRect
Creates a bitmap image using the data contained within a subregion of an existing bitmap image.
You can achieve what you want with a method such as this one :
- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:1.0 orientation:image.imageOrientation];
CGImageRelease(newImageRef);
return newImage;
}
Hope this helps,
Vincent

Help on capturing screen

i want to take screenshots in landscape mode.
currently my below code takes screenshots in PORTRAIT mode.
also i want to store the images into the given location not in photo library..
how can i attain this...
thanks for any help
below is my code
UIGraphicsBeginImageContext(self.view.bounds.size) ;
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);
As far as I know there is no way to take screenshots in landscape mode. Images taken with the iPhone contain an information called imageOrientation which is then used to rotate an UIImageView displaying that image.
But you should be able to take an image in portrait mode and rotate it by 90 degree before saving it. I can't try it right now but the following should work:
Create a method for rotation and pass the UIImage as an argument.
CGSize size = sizeOfImage;
UIGraphicsBeginImageContext(size);
CGContextRotateCTM(ctx, angleInRadians); // (M_PI/2) or (3M_PI/2) depending on left/right rotation
CGContextDrawImage(UIGraphicsGetCurrentContext(),
CGRectMake(0,0,size.width, size.height),
image);
UIImage *copy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return copy;
To store into a given location you can use NSData as I have answered on your other question ( Saving images to a given location )

Using CGContext to display a sprite sheet iPhone

So I have a spritesheet in png format, and have already worked out the coordinates for what I want to display. I'm trying to create a method that will return a UIImage when passed the location information on the spritesheet. I'm just not sure how to use the CGContext stuff along with the the coordinates to return an UIImage.
I was looking to CGContextClipToRect because I think that is the way to do it, but I just can't seem to get it to work.
Something like this
CGSize size = CGSizeMake(xSize, ySize);
UIGraphicsBeginImageContext(size);
//CGContextClipToRect(spriteSheet.size, imageRect);
[spriteSheet drawAtPoint:CGPointMake(xPos, yPos)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
returns only what is in the size Context. I need to be able to "move" this size window around the spritesheet if that makes sense.
Any help would be appreciated.
Thanks,
Mike
I think the call you want to use is CGImageCreateWithImageInRect
newImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect( [spriteSheet CGImage] , imageRect )];
you need to think of your co-ordinates backwards. The CGSize is fixed. Translate your xPos,yPos so that your sprite appears under the window.