简体   繁体   English

UIImageView裁剪在设备上显示错误

[英]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) (第一张图片来自模拟器,第二张图片来自iphone设备)

// 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);

这是它在模拟器上显示的方式(正确)

它在设备上的外观

The problem here is that retina devices are 2x the size of normal devices. 这里的问题是视网膜设备的尺寸是普通设备的2倍。 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. 并根据返回的BOOL值增加/减小rect的大小。

Note* displayLinkWithTarget:selector: is just a random method that works in iOS 4.0+ but not previous versions. 注意* displayLinkWithTarget:selector:只是一个随机方法,适用于iOS 4.0+但不适用于以前的版本。 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

如果你想简化你可能使用的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM