2014-05-18 20 views
3

我想将图像以圆形图像的形式保存到iOS中的相册中。我试图以圆形掩盖图像,并且在图像视图中工作正常,但是当我保存同一图像时,它将被保存为原始图像。IOS将图像保存为cameraRoll中的圆圈

这里是我的代码:

@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 [email protected][imageview.image]; 

UIActivityViewController *activityVC =[[UIActivityViewController alloc]initWithActivityItems:itemsToShare applicationActivities:nil]; 

activityVC.excludedActivityTypes [email protected][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];} 

`

回答

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 [email protected][[self imageWithRoundedCornersSize:self.imageview.image.size.width*2 usingImage:self.imageview.image]]; 

    UIActivityViewController *activityVC =[[UIActivityViewController alloc]initWithActivityItems:itemsToShare applicationActivities:nil]; 

    activityVC.excludedActivityTypes [email protected][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]; 
} 

仅供参考此图像舍入函数适用于正方形图像,与矩形图像不太好。对于矩形图像,您必须以圆形方式裁剪图像的一部分并绘制新图像。

+0

试过这个仍然保存为正常图像请别人帮我 – anjani

+0

仍然没有工作,我可以发送我的文件,你可以给我们的电子邮件ID请看看它会有所帮助。, – anjani

+0

satheesh4590 @ gmail。 com – satheeshwaran