我想将图像保存为圆形图像到iOS中的相册。我尝试将图像屏蔽为圆形,并且在图像视图中工作正常,但是当我保存相同的图像时,它会被保存为原始图像。
这是我的代码:
@implementation ViewController
@synthesize imageview, photo;
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageview.layer.masksToBounds = YES;
self.imageview.layer.cornerRadius = imageview.bounds.size.width/2;
self.imageview.layer.borderWidth = 1.0;
self.imageview.layer.borderColor =[UIColor colorWithRed:13/255 green:70/255 blue:131/255 alpha:1.0].CGColor;}
-(IBAction)takephoto:(id)sender{
picker1 = [[UIImagePickerController alloc]init];
picker1.delegate = self;
[picker1 setSourceType:(UIImagePickerControllerSourceTypeCamera)];
[self presentViewController:picker1 animated:YES completion:NULL];
}
-(IBAction)chooseExisting:(id)sender{
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate = self;
[picker2 setSourceType:(UIImagePickerControllerSourceTypePhotoLibrary)];
[self presentViewController:picker2 animated:YES completion:NULL];}
- (IBAction)save:(id)sender {
//NSString *shareText = @"download my add with the follow up link";for textwrking.
NSArray *itemsToShare =@[imageview.image];
UIActivityViewController *activityVC =[[UIActivityViewController alloc]initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes =@[UIActivityTypePostToFacebook /* this is like excuding activities like fb,twitterand all whtever we login on ios, we can use this code */];
[self presentViewController:activityVC animated:YES completion:nil];}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
photo=[info objectForKey:UIImagePickerControllerOriginalImage];
[imageview setImage:photo];
[self dismissViewControllerAnimated:YES completion:NULL];}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];}
`
答案 0 :(得分:6)
在不使用imageview的情况下尝试此解决方案。
- (UIImage *)imageWithRoundedCornersSize:(float)cornerRadius usingImage:(UIImage *)original
{
CGRect frame = CGRectMake(0, 0, original.size.width, original.size.height);
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContextWithOptions(original.size, NO, 1.0);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:frame
cornerRadius:cornerRadius] addClip];
// Draw your image
[original drawInRect:frame];
// Get the image, here setting the UIImageView image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return image;
}
- (IBAction)share:(id)sender {
//NSString *shareText = @"download my add with the follow up link";for textwrking.
NSArray *itemsToShare =@[[self imageWithRoundedCornersSize:self.imageview.image.size.width*2 usingImage:self.imageview.image]];
UIActivityViewController *activityVC =[[UIActivityViewController alloc]initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes =@[UIActivityTypePostToFacebook /* this is like excuding activities like fb,twitterand all whtever we login on ios, we can use this code */];
[self presentViewController:activityVC animated:YES completion:nil];
}
仅供参考这个图像舍入功能与方形图像一起使用效果非常好,对矩形图像效果不佳。对于矩形图像,您必须以圆形方式裁剪图像的一部分并绘制新图像。