【问题标题】:Image is not fit into circle UIImageview in iOS Objective-C?图像不适合 iOS Objective-C 中的圆形 UIImageview?
【发布时间】:2018-01-28 06:20:24
【问题描述】:

我知道这是一个常见问题,但我有能力实施它。我有一个体重和身高相等的图像视图。当我从媒体库中选择图像时,有些图像适合圆形,但有些图像不适合圆形图像视图。

谁能建议我如何将任何类型的图像集设置为圆形图像视图?

因为我想实现用户可以设置他/她的个人资料图像。所以用户插入任何类型的图像或任何分辨率,我需要裁剪该图像但我无法做到这一点.而且我也不喜欢任何第三个图书馆。

这是我的代码:

-(void)btnImgAct //Button Action
 {
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    imagePickerController.delegate = self;

    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypePhotoLibrary])
    {

        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    }

    [self presentViewController:imagePickerController animated:YES completion:nil];
}


   - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)Info
 {

    UIImage  *pickerImage=[[UIImage alloc]init];

    pickerImage=image;

    imageDataPicker =[[NSData alloc] init];
    imageDataPicker = UIImageJPEGRepresentation(pickerImage, 0.1); //For resize


    if([imageDataPicker length]<2097152) //bytes 1048576
    {


        [[picker parentViewController] dismissModalViewControllerAnimated:YES];
        [self dismissViewControllerAnimated:YES completion:nil];
        picker=nil;


        [self SubmitImage1:pickerImage];



    }


-(void)SubmitImage1:(UIImage *)image
   {

EditProfileImage.image=image; //insert image into imageview

  //Create circle imageview
EditProfileImage.layer.cornerRadius = EditProfileImage.frame.size.width / 2;
[EditProfileImage setContentMode:UIViewContentModeScaleAspectFit];
EditProfileImage.layer.masksToBounds = YES;

EditProfileImage.layer.borderWidth = 3.0f;
EditProfileImage.layer.borderColor = [UIColor blackColor].CGColor;

 }

enter image description here

enter image description here

【问题讨论】:

  • 到底是什么问题?是不是你种植不好?在这种情况下,请尝试改用内容模式 UIViewContentModeScaleAspectFill。这将用图像填充你的圆圈。
  • @LGP,当我从库中插入图像时,这是任何图像适合圆形视图但某些图像不适合圆形的任何结果。你也可以建议我如何辞职的形象。我的 imageview 体重和身高是 100.0f。
  • 您能否展示一张不合适的图片的外观?
  • 嗨@LGP,我已经更新了我的问题。请看上面的图片链接。请提出任何想法,因为我不知道为什么我会显示不需要的问题。

标签: objective-c uiimageview xcode8 ios9


【解决方案1】:

删除了一些不必要的行并更改了图像视图以对生成的图像进行方面填充。我已经对此进行了测试,它适用于我尝试过的任何图像。

-(IBAction)buttonPressed:(UIButton *)sender
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)Info
{
    NSData *imageDataPicker = UIImageJPEGRepresentation(image, 0.1); //For resize

    if([imageDataPicker length]<2097152) //bytes 1048576
    {
        [self dismissViewControllerAnimated:YES completion:nil];
        [self SubmitImage1:image];
    }
}

// UIViewContentModeScaleAspectFill will fill the entire view
-(void)SubmitImage1:(UIImage *)image
{
    _EditProfileImage.image = image;
    _EditProfileImage.layer.cornerRadius = _EditProfileImage.frame.size.width / 2;
    _EditProfileImage.contentMode = UIViewContentModeScaleAspectFill;
    _EditProfileImage.layer.masksToBounds = YES;
    _EditProfileImage.layer.borderWidth = 3.0f;
    _EditProfileImage.layer.borderColor = [UIColor blackColor].CGColor;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多