Objective C - 在 UIButton 上设置的来自相机的图像

Objective C - Image from camera to set on UIButton

我正在尝试将来自相机的图像设置为在我的按钮上设置,但默认情况下它不接受裁剪图像,它设置为相机捕获的原始图像宽度和高度,并且由于此图像看起来从顶部缩小到底部 .

当我点击 UIButton 时,相机打开,然后在拍摄图像后显示在此 UIButton

- (IBAction)ProfImageBtnCliked:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    [picker setAllowsEditing:YES];
    picker.delegate = self;
    if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear] || [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]  )
    { 
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        [self presentViewController:picker animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"No Camera Available"delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show]; 
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:Nil];
    NSData *dataForImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"], 1.0);
    [ProfImgButton setImage:[UIImage imageWithData:dataForImage] forState:UIControlStateNormal];
    ProfImgButton.userInteractionEnabled=NO;
    ProfImgButton.contentMode=UIViewContentModeScaleAspectFill;
    ProfImgButton.clipsToBounds=YES;
    CGSize constraint = CGSizeMake(200,200);
    NSData *imgData = UIImageJPEGRepresentation([self imageWithImage [UIImage imageWithData:dataForImage] scaledToSize:constraint], 0);
    NSLog(@"Size of Image(bytes):%lu",(unsigned long)[imgData length]);
    NSString *imageString = [imgData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    NSLog(@"%@",imageString);

    //assign base64 image to image string
    Base64ImageString=[imageString stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];    
}

//reduce image size
-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

你的裁剪代码有误!

这样写:-

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
        {
            [picker dismissViewControllerAnimated:YES completion:nil];
               CGSize size = CGSizeMake(200,200);

              UIImage *originalImage=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerEditedImage"], 1.0);

              //CROP image code
               UIGraphicsBeginImageContext(size);   
              [originalImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
              UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
              UIGraphicsEndImageContext();

            //now assign image to button 
            [ProfImgButton setImage:croppedImage  forState:UIControlStateNormal];
            ProfImgButton.userInteractionEnabled=NO;
            ProfImgButton.contentMode=UIViewContentModeScaleAspectFill;
            ProfImgButton.clipsToBounds=YES;


            NSData *imgData = UIImageJPEGRepresentation(croppedImage , 0);
            NSLog(@"Size of Image(bytes):%lu",(unsigned long)[imgData length]);
            NSString *imageString = [imgData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
            NSLog(@"%@",imageString);


        //assine base64 image to image string
            Base64ImageString=[imageString stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];    
  }

您正在此处的代码中设置从相机拍摄的原始图像

[picker dismissViewControllerAnimated:YES completion:Nil];
    NSData *dataForImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerEditedImage"], 1.0);
    [ProfImgButton setImage:[UIImage imageWithData:dataForImage] forState:UIControlStateNormal];

您正在调整下图的大小时

CGSize constraint = CGSizeMake(200,200);
    NSData *imgData = UIImageJPEGRepresentation([self imageWithImage [UIImage imageWithData:dataForImage] scaledToSize:constraint], 0);
    NSLog(@"Size of Image(bytes):%lu",(unsigned long)[imgData length]);

此数据未在任何地方使用。您没有在 ProfImgButton

上设置它

您需要做的就是将调整大小的图像数据设置到您的按钮

[ProfImgButton setImage:[UIImage imageWithData:imgData] forState:UIControlStateNormal];