2012-10-09 101 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

相關問題