4

In iOS, my UIImageView is "Aspect Fit", but when I do CIFilter of type e.g. "CISepiaTone", I lose the original image aspect ratio. The image is stretched to whole UIImageView size.

How can I preserve the aspect ratio?

user1118321
  • 25,567
  • 4
  • 55
  • 86
mlev
  • 181
  • 7
  • note: I tried (from http://stackoverflow.com/questions/7645454/resize-uiimage-by-keeping-aspect-ratio-and-width) + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } but sometimes it rotates images by 90 degrees... – mlev Aug 14 '13 at 21:32

1 Answers1

4

If you are creating the filtered image with imageWithCIImage, then you won't get the proper aspect ratio. Try creating a CGImageRef from the filter output CIImage and then creating the UIImage from the CGImageRef.

Example:

// assume you already have CIImage *outputImage and *inputImage declared

// convert to CGImage to maintain proper aspect ratio and dimensions
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef imageRef = [context createCGImage:outputImage fromRect:inputImage.extent];
UIImage *filteredImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

Then you can try setting the imageView with filteredImage.