14

I have a small IPhone app that I am working on and I am displaying an image with a UIImageView. I am scaling it up using the Aspect Fit mode. I would like to have the image scale up with no interpolation/smoothing (I want it to look pixellated). Is there any way I can change this behavior?

A little more general question would be can I implement my own scaling algorithm, or are there other built in ones that I can select?

Dolphin
  • 4,655
  • 1
  • 30
  • 25

4 Answers4

24

You would need to set the magnificationFilter property on the view's layer to the nearest neighbour filter:

[[view layer] setMagnificationFilter:kCAFilterNearest]
rpetrich
  • 32,196
  • 6
  • 66
  • 89
  • 1
    as rekle's answer says, make sure you add #import when using setMagnificationFilter: – Niklas Berglund Jun 06 '12 at 20:14
  • 1
    That's great and achieves my purpose, but I want to ask, when I use `magnificationFilter = kCAFilterNearest`, are the big and sharp pixels magnified really the original pixels of the image, or just produced by some processing?Thank you! – Suge Nov 30 '14 at 01:24
5

Make sure you include the line

#import  <QuartzCore/CALayer.h>

At the top of the file containing the call to setMagnificationFilter or it won't compile.

rekle
  • 2,375
  • 1
  • 18
  • 9
  • And add QuartzCore framework to your project (Groups & Files -> Add -> ExistingFrameworks... -> QuartzCore.framework -> Add). – suda Feb 16 '11 at 18:05
5

Swift

imageView.layer.magnificationFilter = kCAFilterNearest
imageView.layer.shouldRasterize = true
neoneye
  • 50,398
  • 25
  • 166
  • 151
-2

There are two ways you could do this. You can either implement a rescaling algrorithm without interpolation. If this is to much work you could rescale the image in another application like gimp or photoshop. This would work unless you needed to change the size dynamically in your application.

DHamrick
  • 8,338
  • 9
  • 45
  • 62