制作屏幕截图时隐藏按钮UIGraphicsBeginImageContextWithOptions

时间:2014-06-05 10:31:40

标签: ios objective-c

有没有办法在使用UIGraphicsBeginImageContextWithOptions制作屏幕截图时隐藏特定按钮(IBAction)?

我正在使用以下代码:

 // Define the dimensions of the screenshot you want to take (the entire screen in this case)
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //now we will position the image, X/Y away from top left corner to get the portion we want
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [sourceImage drawAtPoint:CGPointMake(0, -20)];
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);

感谢您的回答!

2 个答案:

答案 0 :(得分:2)

为视图中的每个按钮添加插座,并设置为隐藏=是,

否则你可以这样做

for(UIView *v in self.view.subviews){
    if([v isKindOfClass:[UIButton class]])
        [v setHidden:YES];
}

答案 1 :(得分:0)

-(IBAction)doButton {

    UIActionSheet *doSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take a Picture", @"Select from Album", nil];

    [doSheet showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex {
    if (buttonIndex == 0) {

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = NO;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:picker animated:YES completion:NULL];

    }
    if(buttonIndex == 1) {

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = NO;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:picker animated:YES completion:NULL];

    }
}


#pragma mark - Image Picker Controller delegate methods

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
    self.imageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];

    for(UIView *v in self.view.subviews){
        if([v isKindOfClass:[UIButton class]])
            [v setHidden:YES];
    }

    // Define the dimensions of the screenshot you want to take (the entire screen in this case)
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //now we will position the image, X/Y away from top left corner to get the portion we want
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [sourceImage drawAtPoint:CGPointMake(0, -20)];
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Share Now"
                                                    message:@"Share or save now bla bla bla"
                                                   delegate:self
                                          cancelButtonTitle:@"No Thanks!"
                                          otherButtonTitles:@"Yes Share",nil];
    [alert performSelector:@selector(show) withObject:nil afterDelay:1.2];

}


// callback for UIImageWriteToSavedPhotosAlbum
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

    if (error) {
        // Handle if the image could not be saved to the photo album
    }
    else {
        // The save was successful and all is well
    }
}