Saving a View as a Photo in iPhone App

后端 未结 1 413
长发绾君心
长发绾君心 2021-01-31 12:57

Is there an easy way to programmatically save a view to the Photos library in an iPhone app?

1条回答
  •  孤城傲影
    2021-01-31 13:33

    I added these methods to my view's controller class. They save the photo and then pop up an alert box to tell the user whether it succeeded.

    - (void)savePhotoOfView:(UIView *)view
    {
        UIGraphicsBeginImageContext(view.bounds.size);
        [view drawRect:view.bounds];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        UIImageWriteToSavedPhotosAlbum(image,
                                       self,
                                       @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),
                                       NULL);
    }
    
    - (void)   savedPhotoImage:(UIImage *)image
      didFinishSavingWithError:(NSError *)error
                   contextInfo:(void *)contextInfo
    {    
        NSString *message = @"This image has been saved to your Photos album";
        if (error) {
            message = [error localizedDescription];
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    

    0 讨论(0)
提交回复
热议问题