2
votes

I created an application, in which I can rotate, re-size, translate an image using gestures. Then I need to get the image from the UIImageView. I found this part of the code at some where in Stack-overflow. Although the smiler question is answered here, but it requires the input of the angle. The same person wrote somewhere else the better solution, which I'm using. But it have a problem. Often it returns a blank image. or truncated image (often from top side). So there is something wrong with the code and it requires some changes. My problem is that, I'm new to Core-graphics and badly stuck in this problem.

UIGraphicsBeginImageContext(imgView.image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform = imgView.transform;
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, CGRectMake(0, 0, imgView.image.size.width, imgView.image.size.height), imgView.image.CGImage);
UIImage *newRotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
UIGraphicsEndImageContext();

EDIT 1.1 Thanks for the sample code, but again it have the problem. Let me explain in more detail, I'm using gestures for scaling, translating and resizing the image using imageview. So all this data is saved in the transform property of the imageview. I fond another method in core-image. So I changed my code to:

CGRect bounds =  CGRectMake(0, 0, imgTop.size.width, imgTop.size.height);
CIImage *ciImage = [[CIImage alloc] initWithCGImage:imageView.image.CGImage options:nil];
CGAffineTransform transform = imgView.transform;
ciImage = [ciImage imageByApplyingTransform:transform];
return [UIImage imageWithCIImage:ciImage] ;

Now I'm getting the squeezed and wrong size mirrored image. Sorry to disturbing you again. Can you guide me how to get the proper image using imageview's transform in coreimage?

4
you can use CoreImage framework to rotate image it is for iOS 5.0 or later but its really better that core graphics..Leena
thanks for the tip. can you provide any link or sample? Sorry I know very little about core image.andy
ok i just provide you sample code and link to the documentation.Leena
Just for the record, I needed merging 2 UIImages and thus I had problem as you - how to get UIImage from UIImageView transformed with CGAffineTransformation. Your code from EDIT 1.1 helped me and works for my purposes.uerceg

4 Answers

3
votes
CIImage *ciImage = [[CIImage alloc] initWithCGImage:fximage.CGImage options:nil];
CGAffineTransform transform = fxobj.transform;
float angle = atan2(transform.b, transform.a);
transform = CGAffineTransformRotate(transform, - 2 * angle);
ciImage = [ciImage imageByApplyingTransform:transform];
UIImage *screenfxImage = [UIImage imageWithCIImage:ciImage];

Do remember to add code : transform = CGAffineTransformRotate(transform, - 2 * angle); coz the rotation direction is opposite

1
votes

I created an objective-C class just for this sort of thing. You can check it out on GitHub ANImageBitmapRep. Here's how you would do rotation:

ANImageBitmapRep * ibr = [myImage image];
[ibr rotate:anAngle];
UIImage * rotated = [ibr image];

Note that here, anAngle is in radians.

1
votes

Here is the link to Documentation:- http://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CoreImageFilterReference/Reference/reference.html

Sample code to rotate image:-

        CIImage *inputImage = [[CIImage alloc] initWithImage:currentImage];
        CIFilter * controlsFilter = [CIFilter filterWithName:@"CIAffineTransform"];
        [controlsFilter setValue:inputImage forKey:kCIInputImageKey];
        [controlsFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputAngle"];
        CIImage *displayImage = controlsFilter.outputImage;
        UIImage *finalImage = [UIImage imageWithCIImage:displayImage];

        CIContext *context = [CIContext contextWithOptions:nil];
        if (displayImage == nil || finalImage == nil) {
            // We did not get output image. Let's display the original image itself.
            photoEditView.image = currentImage;
        }
         else {

            CGImageRef imageRef = [context createCGImage:displayImage fromRect:displayImage.extent];
            photoEditView.image = [UIImage imageWithCGImage:imageRef];
            CGImageRelease(imageRef);
        }
        context = nil;
        [inputImage release];
0
votes

I created sample app to do this (minus the scaling part) in objective C. If anybody is interested, you can download it here: https://github.com/gene-code/coregraphics-drawing/tree/master/coregraphics-drawing/test