user3615707 user3615707 - 3 months ago 64
iOS Question

UIImagePickerController - crop picture to circle

Ho do I crop circle images with Image picker controller?

The pictures I'm displaying in my projects are round using the code below

CALayer *image = profileImageView.layer;
[image setCornerRadius:50];
[image setMasksToBounds:YES];

[image setBorderColor:[[UIColor whiteColor]CGColor]];
[image setBorderWidth:5];


So far I have this code to crop images but it only makes them square

- (void)showPhotoLibary
{
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)) {
return;
}

UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

// Displays saved pictures from the Camera Roll album.
mediaUI.mediaTypes = @[(NSString*)kUTTypeImage];

// Hides the controls for moving & scaling pictures
mediaUI.allowsEditing = YES;


mediaUI.delegate = self;


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


- (void) imagePickerController: (
UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {

UIImage *originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
self.profileImageView.image = originalImage;



[picker dismissViewControllerAnimated:YES completion:nil];

}

Answer

Check if u added QuartzCore framework to your project. then u can do somthing like below import #import <QuartzCore/QuartzCore.h> .m or .h file

- (void)viewDidLoad
 {
   [self.profileImageView.layer setCornerRadius:50];
   [self.profileImageView.layer setMasksToBounds:YES];
   [self.profileImageView.layer setBorderColor:[[UIColor whiteColor]CGColor]];
   [self.profileImageView.layer setBorderWidth:5];
 }

- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info 
 {
   UIImage *originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage]; //it returns the edited image, 
   self.profileImageView.image = originalImage;
   [picker dismissViewControllerAnimated:YES completion:nil];

 }
Comments