Rory Lester
Rory Lester

Reputation: 2918

UIImageView cropped being displayed wrong on device

I have an image and i am cropping part of it. The problem is that in the simulator it is displayed correctly, but on the device it is much more zoomed in. It's quite a bit difference. What am i doing wrong? (first image is from the simulator and second from the iphone device)

// create bounds and initialise default image 
CGRect imageSizeRectangle = CGRectMake(0, 0, 300, 300);
UIImage *df_Image =  [UIImage imageNamed:@"no_selection.png"];
self.imageView = [[UIImageView alloc] initWithFrame:imageSizeRectangle];
[imageView setImage:df_Image];
[self.view addSubview:imageView];

//crop image 
CGRect test = CGRectMake(0, 0, 150,150);
CGImageRef imageRef = CGImageCreateWithImageInRect([photo.image CGImage], test);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

This is how it is displayed on the simulator (correctly)

How it looks on the device

Upvotes: 0

Views: 645

Answers (2)

Ali Kıran
Ali Kıran

Reputation: 1006

if you want to simplize your code you may use

CGRectMake(0,0,[UIScreen mainScreen].scale*150,[UIScreen mainScreen].scale*150)

Upvotes: 1

ohr
ohr

Reputation: 1727

The problem here is that retina devices are 2x the size of normal devices. You could check if the device is retina or not with the following method;

+(BOOL)iPhoneRetina{
return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))?1:0;
}

And increase/decrease the size of your rect according to the BOOL value returned.

Note* displayLinkWithTarget:selector: is just a random method that works in iOS 4.0+ but not previous versions. You don't need to pay much attention to it.

Edit*

CGRect rect;
if([self iPhoneRetina]){rect = CGRectMake(0,0,300,300);}//Retina
else{rect = CGRectMake(0,0,150,150);}//Non retina

//Then the rest of your code

Upvotes: 1

Related Questions