簡體   English   中英

在iOS中調整圖像大小並保持其縱橫比

[英]Resize image and maintain its aspect ratio in iOS

我正在嘗試從照片庫中選擇圖像,這是照片 在此處輸入圖片說明 我是從網上下載的,並存儲在我的照片庫中。

所以現在我想給用戶一個選項,可以從照片庫中選擇一個圖像,並在我的應用程序中作為backgroundImage進行應用,但是當我這樣做時,我會以這種方式在iphone屏幕上獲取圖像

在此處輸入圖片說明

實際上,由於指定的內容模式為“ scaleToFill”,因此圖像已經填滿了屏幕。

1)我想知道如何使此圖像保持其縱橫比並填充屏幕?

問候蘭吉特

檢查這個博客 使用這兩個文件

的UIImage + Resize.h

的UIImage + Resize.m

UIImage *image = [UIImage imageNamed:@"SomeImage-900x675"]; // SomeImage-900x675.png 
CGFloat targetWidth = 320.0;
CGFloat scaleFactor = targetWidth / image.size.width; 
CGFloat targetHeight = image.size.height * scaleFactor; 
CGSize targetSize = CGSizeMake(targetWidth, targetHeight);
UIImage *scaledImage = [image resizedImage:targetSize interpolationQuality:kCGInterpolationHigh];

盡管您的圖片占據了所有屏幕尺寸,但是您的圖片尺寸卻更大了...您可以嘗試按以下分辨率縮放圖片320 * 480、640 * 960、1240 * 2208

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM