UIImageview 在获取图像后在静态 table 视图单元格中不保留其圆形
UIImageview does not retain its round shape in static table view cell after getting image in it
我想在图像视图中设置从画廊和相机拍摄的图像,该图像视图是圆形的 shape.But 当我将图像设置到图像视图时,图像视图的形状会发生变化。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//make imageview round
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
imageView.clipsToBounds = YES;
UIImage *resizedImage=[Services resizeImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image=resizedImage;
[self dismissViewControllerAnimated:true completion:nil];
提前致谢
您也可以反其道而行之:将图片裁剪成圆形,然后将其添加到方形视图中:
CALayer *imageLayer = image.layer;
[imageLayer setCornerRadius:image.frame.size.width/2];
[imageLayer setBorderWidth:1];
[imageLayer setMasksToBounds:YES];
imageLayer.borderColor = (__bridge CGColorRef _Nullable)([UIColor clearColor]);
首先确保您的 imageView 宽高比为 1:1
设置图片大小后设置imageView的cornerRadius
将图像视图框架设置为正方形,然后将圆角半径设置为 width.See 这些线的一半 -
userimage.layer.borderWidth = 3.0
userimage.clipsToBounds = true
userimage.layer.cornerRadius = userimage.frame.size.width / 2;
userimage.layer.borderColor = UIColor.orangeColor().CGColor
此处用户图片尺寸为50*50
您似乎选择了 "tall" 图片。
替换
imageView.contentMode = UIViewContentModeScaleAspectFit;
有
imageView.contentMode = UIViewContentModeScaleAspectFill;
以便它覆盖整个区域,您将能够看到它是圆形的。
我想在图像视图中设置从画廊和相机拍摄的图像,该图像视图是圆形的 shape.But 当我将图像设置到图像视图时,图像视图的形状会发生变化。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//make imageview round
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
imageView.clipsToBounds = YES;
UIImage *resizedImage=[Services resizeImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image=resizedImage;
[self dismissViewControllerAnimated:true completion:nil];
提前致谢
您也可以反其道而行之:将图片裁剪成圆形,然后将其添加到方形视图中:
CALayer *imageLayer = image.layer;
[imageLayer setCornerRadius:image.frame.size.width/2];
[imageLayer setBorderWidth:1];
[imageLayer setMasksToBounds:YES];
imageLayer.borderColor = (__bridge CGColorRef _Nullable)([UIColor clearColor]);
首先确保您的 imageView 宽高比为 1:1
设置图片大小后设置imageView的cornerRadius
将图像视图框架设置为正方形,然后将圆角半径设置为 width.See 这些线的一半 -
userimage.layer.borderWidth = 3.0
userimage.clipsToBounds = true
userimage.layer.cornerRadius = userimage.frame.size.width / 2;
userimage.layer.borderColor = UIColor.orangeColor().CGColor
此处用户图片尺寸为50*50
您似乎选择了 "tall" 图片。 替换
imageView.contentMode = UIViewContentModeScaleAspectFit;
有
imageView.contentMode = UIViewContentModeScaleAspectFill;
以便它覆盖整个区域,您将能够看到它是圆形的。