[英]How to Define UIImageView size as UIImage resolution?
我有場景,我使用Web服務獲取圖像,所有圖像都有不同的分辨率。 現在我的要求是我想要分辨每個圖像並使用我想要定義UIImageView的大小以便我可以防止我的圖像變得模糊
例如,圖像分辨率如果326像素/英寸,則圖像視圖應該與該圖像的大小完全無代表地表示。
UIImage *img = [UIImage imageNamed:@"foo.png"];
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
UIImageView *imgView = [[UIImageView alloc] initWithFrame:rect];
[imgView setImage:img];
圖像大小是它的分辨率。
你的問題可能是 - 視網膜顯示!
檢查Retina顯示因此 - 使UIImageView 寬度/高度減小兩倍 (這樣每個UIImageView像素將由四個較小的UIImage像素組成,用於視網膜顯示)。
如何檢查視網膜顯示:
https://stackoverflow.com/a/7607087/894671
如何檢查圖像大小(實際上沒有在內存中加載圖像):
NSString *mFullPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
stringByAppendingPathComponent:@"imageName.png"];
NSURL *imageFileURL = [NSURL fileURLWithPath:mFullPath];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
if (imageSource == NULL)
{
// Error loading image ...
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache, nil];
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
NSNumber *mImgWidth;
NSNumber *mImgHeight;
if (imageProperties)
{
//loaded image width
mImgWidth = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
//loaded image height
mImgHeight = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
CFRelease(imageProperties);
}
if (imageSource != NULL)
{
CFRelease(imageSource);
}
所以 - 例如:
UIImageView *mImgView = [[UIImageView alloc] init];
[mImgView setImage:[UIImage imageNamed:@"imageName.png"]];
[[self view] addSubview:mImgView];
if ([UIScreen instancesRespondToSelector:@selector(scale)])
{
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1.0)
{
//iphone retina screen
[mImgView setFrame:CGRectMake(0,0,[mImgWidth intValue]/2,[mImgHeight intValue]/2)];
}
else
{
//iphone screen
[mImgView setFrame:CGRectMake(0,0,[mImgWidth intValue],[mImgHeight intValue])];
}
}
希望有所幫助!
您可以使用以下代碼獲取圖像大小。 因此,首先計算下載的圖像大小,然后根據它來制作圖像視圖。
UIImage *Yourimage = [UIImage imageNamed:@"image.png"];
CGFloat width = Yourimage.size.width;
CGFloat height = Yourimage.size.height;
希望這個能對您有所幫助..
UIImage *oldimage = [UIImage imageWithContentsOfFile:imagePath]; // or you can set from url with NSURL
CGSize imgSize = [oldimage size];
imgview.frame = CGRectMake(10, 10, imgSize.width,imgSize.height);
[imgview setImage:oldimage];
100%工作....
讓UIImageView通過利用contentMode屬性為您的圖像調整大小來完成工作。
您可能希望使用靜態大小(“frame”屬性)顯示UIImageView,該大小表示要顯示的圖像的最大大小,並允許圖像相對於其自身的特定大小要求在該幀內調整大小(整體尺寸,縱橫比等)。 您可以通過掌握contentMode屬性讓UIImageView為您處理不同大小的圖像做繁重的工作。 它有許多不同的設置,其中一個是UIViewContentModeScaleAspectFit,它會根據需要縮小你的圖像以適應UIImageView,如果圖像較小,它將只顯示居中。 您可以使用該設置來獲得所需的結果。
請注意,使用此方法,您無需處理與Retina顯示相關的縮放問題。
要解決此問題,我們需要注意設備的顯示分辨率。 。
例如,你有一張326ppi 分辨率的圖像,它與iPhone4,iPhone4S和iPod 4th Gen相同。所以你可以簡單地使用@Nit和@Peko建議的解決方案。 但對於其他設備(或這些設備上具有不同分辨率的圖像),您需要應用數學來計算尺寸以便更好地顯示。
現在假設您有260ppi (尺寸為W x H)圖像,並且您希望在iPhone4S上顯示它,因此每英寸中包含的信息小於iPhone的顯示分辨率,因此我們需要通過縮小圖像大小來調整它的大小由326/260因素。 所以現在你將使用的imageView的大小
imageViewWidth = W*(260/326);
imageViewHeight = H*(260/326);
一般來說:
resizeFactor = imageResolution/deviceDisplayResolution;
imageViewWidth = W*resizeFactor;
imageViewHeight = H*resizeFactor;
在這里我正在考慮當我們在imageView中設置圖像並調整其大小時,它不會從圖像中刪除或添加像素,
根據您在問題正文中所述的要求,我相信您無需更改UIImageView大小。
使用以下代碼行,圖像可以完全沒有任何模糊:
imageView.contentMode = UIViewContentModeScaleAspectFit;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.