简体   繁体   English

使大图像适合小尺寸的图像视图

[英]Make a large image fit into a imageview of small size

I am trying to make a large image fit into an UIImageView of small size. 我试图使大图像适合小尺寸的UIImageView How can I reduce the size of the image or scale it appropriately so that the full image fits into the UIImageView ? 如何缩小图像大小或适当缩放图像,使整个图像适合UIImageView

try to use different modes like scale to fill , aspect fit , aspect fill` etc. 尝试使用不同的模式,例如scale to fillaspect fit ,宽高比填充等。

i think accorging to your requirement scale to fill is good. 我认为按照您的需求量scale to fill是好的。

aspect fit will keep the original shape your image and fit accorging to aspect ratio so it will possible that some portion of your imageview remains blank or not cover by image. aspect fit将保持图像的原始形状并根据宽高比进行拟合,因此imageview的某些部分可能保持空白或未被图像覆盖。

Scale to fit try to scale image according to imageview. 缩放以适合尝试根据imageview缩放图像。 this is by default mode. 这是默认模式。

aspect fill will fill whole imageview with aspect ratio. 宽高比填充将以宽高比填充整个imageview。

so choose whatever is best for you. 所以选择最适合您的东西。 you can easily change mode from storyboard from attribute inspector. 您可以从属性检查器的情节提要中轻松更改模式。

hope this will help :) 希望这会有所帮助:)

Set the desired frame of your imageView and set the content mode of the image View. 设置所需的imageView框架并设置图像View的内容模式。 For example : 例如 :

imgView.contentMode = UIViewContentMode.ScaleAspectFit
//OR 
imgView.contentMode = UIViewContentMode.ScaleAspectFill
//OR
imgView.contentMode = UIViewContentMode.ScaleToFill

Use whichever works for you. 使用任何适合您的方法。

By looking at the JSON I'm updating the answer: 通过查看JSON,我正在更新答案:

As I had suspected, its a URL which you are getting in JSON. 正如我所怀疑的那样,它是您在JSON中获取的URL。 So do the following: 因此,请执行以下操作:

  1. Parse the JSON and get the URL from the JSON.(videothumbnail key value) 解析JSON并从JSON获取URL。(videothumbnail键值)
  2. The URL contains back slashes as escape charecters(http://content.m-tutor.com/images/20150916113347_5-cont.png) remove them to get the URL as this http://content.m-tutor.com/images/20150916113347_5-cont.png 该URL包含反斜杠作为转义符(http://content.m-tutor.com/images/20150916113347_5-cont.png)删除它们以获取URL,因为此http://content.m-tutor.com/images /20150916113347_5-cont.png

  3. Download the image Using below code : 使用以下代码下载图像:

     func startDownloadImageWithURL(urlInString: String?){ let session = NSURLSession.sharedSession(); if let urlInString = urlInString{ let urlToDownload = NSURL(string: urlInString) session.dataTaskWithURL(urlToDownload!, completionHandler: {(data : NSData?,response : NSURLResponse?, error : NSError?) -> Void in dispatch_async(dispatch_get_main_queue(), { if let error = error { print("Error while downloading \\(error.localizedDescription)") } else{ //Assuming the yourImageView is already initialized with position //yourImageView.image = UIImage(data: data) } }) }).resume() } } 

I have given just an example on how to download image(get the data), create the image using data downloaded and the set it to imageView, you can update it according to your need. 我只是给出了一个有关如何下载图像(获取数据),使用下载的数据创建图像并将其设置为imageView的示例,您可以根据需要进行更新。 Hope it helps :] 希望能帮助到你 :]

将imageView的模式设置为Aspect Fit

为您的图像视图设置此属性

imageView.contentMode  = UIViewContentModeScaleAspectFit;

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

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