我可以使用Image picker controller裁剪圆形图像吗?
我在项目中显示的图片是圆形的,使用下面的代码
CALayer *image = profileImageView.layer;
[image setCornerRadius:50];
[image setMasksToBounds:YES];
[image setBorderColor:[[UIColor whiteColor]CGColor]];
[image setBorderWidth:5];
到目前为止,我有这个代码来裁剪图像,但它只是使它们成为正方形
- (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];
}
答案 0 :(得分:2)
导入#import <QuartzCore/QuartzCore.h>
inyour.h文件
self.profileImageView.layer.cornerRadius = imageView.frame.size.width/2; //or 20.0
self.profileImageView.layer.masksToBounds = YES;
答案 1 :(得分:1)
导入&#34; QuartzCore框架&#34;访问&#34; .cornerRadius
属性&#34;您的UIView
或UIImageView
。
#import <QuartzCore/QuartzCore.h>
还可以手动将其添加到项目的Frameworks文件夹中。将此方法添加到视图控制器或您需要的任何位置:
-(void)setRoundedView:(UIImageView *)roundedView toDiameter:(float)newSize;
{
CGPoint saveCenter = roundedView.center;
CGRect newFrame = CGRectMake(roundedView.frame.origin.x, roundedView.frame.origin.y, newSize, newSize);
roundedView.frame = newFrame;
roundedView.layer.cornerRadius = newSize / 2.0;
roundedView.center = saveCenter;
}
答案 2 :(得分:0)
如果要显示圆圈UIImageView,可以按照以下步骤进行操作
profileImageView.layer.cornerRadius = imageView.frame.size.width/2;
profileImageView.clipsToBounds = YES;
如果要将原始图像源更改为圆形,则必须执行一些图像绘制过程。
答案 3 :(得分:0)
检查您是否已将QuartzCore framework
添加到项目中。
然后你可以做下面的事情
导入#import <QuartzCore/QuartzCore.h>
.m或.h文件
- (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];
}