In the below shown image i am getting the images from web services and passing it to a table view but when scrolling up and down the image size was increasing and it is overlapping on labels and i had given constraints also can anyone help me how to avoid this ?
-
resize your image before load – Anbu.Karthik Jul 21 '17 at 06:37
-
how to resize the image ? @Anbu.Karthik – Vamsi S Jul 21 '17 at 06:37
-
https://stackoverflow.com/questions/17018617/how-to-resize-an-image-in-ios. check this for resize the image. – Jul 21 '17 at 06:38
-
can anyone post the answer for swift it is in objective c @VishalKalola – Vamsi S Jul 21 '17 at 06:40
-
https://objectivec2swift.com/ convert code into swift. – Jul 21 '17 at 06:41
3 Answers
Hey you don't need to resize image.
- First Set fix height width of your image view with constraints in tableview cell
Second Set imageview to aspectFit.
imageView.contentMode = UIViewContentModeScaleAspectFit;
Constraints add like this of your image view
Hope you will get success using that, if you any query regarding this , just comment will help

- 6,394
- 2
- 28
- 35
-
before itself i had given the height constraint but when i given image view aspect fit also still problem continues just see my image https://i.stack.imgur.com/30ioV.png – Vamsi S Jul 21 '17 at 06:49
-
@VamsiKrishna .. Please also set fix width of your imageview , Because its still have auto resizing in width part. So please fix width – Jogendra.Com Jul 21 '17 at 06:50
-
Please try once more .. Remove all cell contraint and set your image view constraints first and after set other constraints – Jogendra.Com Jul 21 '17 at 06:53
-
https://i.stack.imgur.com/onYm4.png these r my constraints for image view – Vamsi S Jul 21 '17 at 06:56
-
Don't set trailing space.. Its mistake .. Constraints are over loop. Please see Image that i have attached in answer – Jogendra.Com Jul 21 '17 at 06:58
-
that trailing for my beside elements i.e is labels and button should i need not to give or not ? – Vamsi S Jul 21 '17 at 07:00
Using content mode to fit the image is an option but if you want to crop or resize it or compress image check the below code.
Call like let imageData = image.compressImage(rate: 0.5)
and then you can provide data to write the image if needed.
func compressImage(rate: CGFloat) -> Data? {
return UIImageJPEGRepresentation(self, rate)
}
OR If you want to crop the image then,
func croppedImage(_ bound: CGRect) -> UIImage? {
guard self.size.width > bound.origin.x else {
print("X coordinate is larger than the image width")
return nil
}
guard self.size.height > bound.origin.y else {
print("Y coordinate is larger than the image height")
return nil
}
let scaledBounds: CGRect = CGRect(x: bound.x * self.scale, y: bound.y * self.scale, width: bound.w * self.scale, height: bound.h * self.scale)
let imageRef = self.cgImage?.cropping(to: scaledBounds)
let croppedImage: UIImage = UIImage(cgImage: imageRef!, scale: self.scale, orientation: UIImageOrientation.up)
return croppedImage
}
Make sure to add the above methods to UIImage Extension.

- 13,198
- 5
- 37
- 71
I had placed a view on table view cell and then I had placed all the elements and given constraints for view and elements in it then my problem has been reduced and working perfectly when scrolling also and is as shown in image below.
here is the layout for this screen

- 269
- 3
- 16