Make UIImage to UIImageView inside UIView

I have a lot of problems getting images according to the correct aspect ratio in their UIImageView.

All images inserted initially from plist into the main data correspond to the correct aspect ratio, but since the images are added using the application (from the existing image library), the images in two of the three views look larger than the view itself - so that only the left is shown top corner of the photo. In the screenshot below, the top row of the table shows the image added via plist, and the next two lines (MY RECORDS) show photos that are not limited to the view.

enter image description here

Cells in the above representation are created in the usual way, and then configured in the native method, where its properties are defined.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) self.configureCell(cell, indexPath: indexPath) return cell } func configureCell(cell: UITableViewCell, indexPath: NSIndexPath) { let record = fetchedResultsController.objectAtIndexPath(indexPath) as! Record cell.textLabel!.text = record.subject cell.detailTextLabel!.text = record.shortDetails if let imageArray = imagesForRecord(record) { if imageArray.count > 0 { cell.imageView?.contentMode = .ScaleAspectFit cell.imageView?.clipsToBounds = true cell.imageView?.image = imageArray[Int(record.featureImageIndex)] } } } 

But the image does not scale and crop within the frame of the image. Initially uploaded images, such as the bear, did not require the installation of contentMode or clipToBounds.

I have the same problem in another view controller (call it VC2), but this view is defined using a storyboard. I have a UIImage view that is bounded by all sides of a UIView with fixed size constraints. The UIImage view is set to Aspect Fit, but when I install the image from the device library, it looks like the images above - the aspect ratio is correct, but only the top corner of the image is displayed.

enter image description here

enter image description here

In another view, I have almost the same folding plan as VC2, but here it works as expected. I do not see differences in the limitations, properties and the same photos (from the library).

enter image description here

enter image description here

I see no difference, and I cannot imagine how to debug from here. Undoubtedly, I do not understand, something rather simple regarding Aspect Fit or how to apply it.

+6
source share
4 answers

Maybe add something like this:

 class func imageWithImage(image: UIImage, scaledToSize newSize: CGSize) -> UIImage { //UIGraphicsBeginImageContext(newSize); UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)) var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } 
0
source

The problem is that you are using the default image image, which does not contain any restrictions. When you upload a larger image, it will be displayed in full size.

Two solutions:

  • Create a cell model, insert your ImageView and give it constraints in the storyboard.
  • Check the restrictions that you added to the default sample.

For instance:

 let horizontalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0) newView.translatesAutoresizingMaskIntoConstraints = false cellView.addConstraint(horizontalConstraint) 
0
source

Try adding this cell.imageView? .Layer.masksToBounds = true. It can help you.

  cell.imageView?.contentMode = .ScaleAspectFit cell.imageView?.clipsToBounds = false cell.imageView?.layer.masksToBounds = true cell.imageView?.image = imageArray[Int(record.featureImageIndex)] 
0
source
  • Create a custom cell with the desired design.

  • Make restrictions in the upper left and lower and set the width for the UIImageView.

  • Add custom Xib using TableView.

Hope this helps you ...

0
source

All Articles