2012-10-09 116 views
0

我试图将图像从滚动视图保存到相册,当用户长按图像时,操作手册显示保存照片按钮,当按下保存按钮时,它不保存图像到相册UIImageWriteToSavedPhotosAlbum没有将图像保存到相册

- (void)viewDidLoad 

{ self.view.backgroundColor = [UIColor blackColor]; 
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
imageScrollView.pagingEnabled = YES; 
NSInteger numberOfViews = 61; 
for (int i = 0; i < numberOfViews; i++) { 
    CGFloat xOrigin = i * self.view.frame.size.width; 
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    [myButton addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside]; 

    myButton.frame = CGRectMake(xOrigin, 10, 60, 35); 

    //[myButton addGestureRecognizer:tap]; 

    [myButton.layer setMasksToBounds:YES]; 

    [myButton.layer setCornerRadius:10.0f]; 

    myButton.layer.borderWidth = 2; 

    myButton.layer.borderColor = [[UIColor whiteColor] CGColor]; 

    [myButton setTitle:@"Done" forState:UIControlStateNormal]; 

    myButton.backgroundColor = [UIColor clearColor]; 

    NSString *imageName = [NSString stringWithFormat:@"image%d.png", i]; 
    UIImage *image = [UIImage imageNamed:imageName]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    _imageView.tag = 128; 
    imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height); 


    UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] 
                 initWithTarget:self 
                 action:@selector(handleLongPress:)]; 

    //imageScrollView.userInteractionEnabled = YES; 
    [imageScrollView addGestureRecognizer:gestureRecognizer]; 
    gestureRecognizer.delegate = self; 
    [gestureRecognizer release]; 

    [imageScrollView addSubview:imageView]; 
    [imageScrollView addSubview:myButton]; 

    //[imageView release]; 

} 
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height); 

[self.view addSubview:imageScrollView]; 
[imageScrollView release]; 
} 



-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
switch (buttonIndex) { 
    case 0: 
    [_imageScrollView viewWithTag:128]; 
    UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);//When i add breakpoint at this statement in result it shows storage zero but alert view says success photo got saved in photo album 

     break; 

    default: 
     break; 
} 
} 

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo 
{ 
if (error != NULL) 
{ 

    alert = [[UIAlertView alloc] initWithTitle:@"Error" 
             message:@"Unable to save image to Photo Album." 
             delegate:self cancelButtonTitle:@"Ok" 
          otherButtonTitles:nil]; 
    } else{ 
     alert = [[UIAlertView alloc] initWithTitle:@"Success" 
              message:@"Image saved to Photo Album." 
              delegate:self cancelButtonTitle:@"Ok" 
           otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    } 
    } 

请大家帮忙。

感谢

+0

你记录什么了吗?为什么你有2个分号;;在你的UIImageWriteToSavedPhotosAlbum函数的末尾? –

+0

原型删除它 – user1452248

+0

您是否收到任何错误消息?很难说只从您提供的代码 –

回答

1

可能与隐私设置的问题 - 人们一直有这个问题,我的应用程序,并报告任何错误消息。理论上,试图保存照片应自动询问用户是否允许应用访问他们的照片,但在某些情况下,用户需要转到设置>隐私>照片,并确保您的应用是允许的。

+0

是的,它是为我的应用程序在一般设置,然后在隐私 – user1452248

相关问题