2

I want to resize the UIImage so I used below code.

UIGraphicsBeginImageContext(size);
[sourceImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Above code works fine but the orientation of destImage is changed. I don't know how.

source.imageOrientation = 3
destImage.imageOrientation = 0

Can any one tell me how to resize image with same orientation as sourceImage?

Edited:

UIImage *scaleImage = [UIImage imageWithCGImage:destImage.CGImage
                                scale:sourceImage.scale orientation:sourceImage.imageOrientation];

Edited:

I am getting this result after resize the image.

I have tried above code also. But still it give me opposite orientation.

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
  • Ekata try another solution – user3182143 Jul 28 '16 at 14:38
  • Possible duplicate of [UIImage orientation is changing when drawing in UIGraphicsBeginImageContext](http://stackoverflow.com/questions/24283017/uiimage-orientation-is-changing-when-drawing-in-uigraphicsbeginimagecontext) – fishinear Jul 28 '16 at 17:32
  • @fishinear I used that code but it didn't work, thats why I asked this question. – Ekta Padaliya Jul 29 '16 at 05:08
  • If you want people to help you, you need to do better than "it didn't work". It works fine for me. State explicitly in your question which code you have used, what you expect it to do, and what it did instead. – fishinear Jul 29 '16 at 09:48
  • Another question: why do you want the `imageOrientation` property of the destination image to be the same as the original? The imageOrientation states how the image must be rotated during drawing, which is exactly what happens when you do drawInRect. So an imageOrientation of 0 for the destination is actually correct. – fishinear Jul 29 '16 at 10:02
  • @fishinear Check my question I have updated. Image orientation is changed after resize when image is large. Got? – Ekta Padaliya Jul 29 '16 at 10:24
  • The image orientation is **not** changed after resize. Check my answer, I hope that will clarify it better to you. – fishinear Jul 29 '16 at 11:29

2 Answers2

1

There is no need to set the orientationMode for the destination image. The orientationMode is an indication how to rotate the image when drawing it. That is, the UIImage contains the original, unrotated, data (usually in CGImage format), and the orientationMode property. When drawing the UIImage (for example when adding it to a UIImageView), it will rotate it as well, as needed.

Your code is also drawing the image, and therefore will rotate it:

[sourceImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();

The destImage will contain the rotated original data, and orientationMode UIImageOrientationUp. The net result is, that drawing the destImage will result in the same image with the same orientation as the sourceImage.

The only difference comes when you actually use the CGImage original data from each (and therefore ignoring the orientationMode property), then the destination image will be a rotated version of the source. If you are going to work with the CGImage anyways, then it's better to use the CGImage level operations to resize the image, for example (this comes from http://fingertwister.tumblr.com/post/9074775714/code-to-create-a-resized-cgimage-from-a-cgimage, I did not verify it):

+ (CGImageRef)resizeCGImage:(CGImageRef)image toWidth:(int)width andHeight:(int)height {
    // create context, keeping original image properties
    CGColorSpaceRef colorspace = CGImageGetColorSpace(image);
    CGContextRef context = CGBitmapContextCreate(NULL, width, height,
        CGImageGetBitsPerComponent(image),
        CGImageGetBytesPerRow(image),
        colorspace,
        CGImageGetAlphaInfo(image));
    CGColorSpaceRelease(colorspace);

    if(context == NULL)
        return nil;

    // draw image to context (resizing it)
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
    // extract resulting image from context
    CGImageRef imgRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);

    return imgRef;
}
fishinear
  • 6,101
  • 3
  • 36
  • 84
-1

Resize the imageView

CALayer *layer;
layer=self.view.layer;
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame); //Give your frame of image here(x,y,width,height)
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Also try below code

UIGraphicsBeginImageContext(self.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
user3182143
  • 9,459
  • 3
  • 32
  • 39