将UIwebview内容保存到照片库

时间:2010-04-02 03:04:04

标签: iphone uiwebview

有一个关于u tube的视频教程,展示了如何执行此操作。它包含一个UIwebview和工具栏按钮来保存内容。没有任何运气使这项工作。可以有人看看,看看他们可以做它工作。非常感谢提前。

http://www.youtube.com/watch?v=gDPca3JIc_s&feature=player_embedded#

///////////////////////////////////////////////////////////////////
//
//  SaveWebViewController.h
//  SaveWeb
//
//  
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SaveWebViewController : UIViewController {
 IBOutlet UIWebView *webview;
} 

@property (nonatomic, retain) IBOutlet UIWebView *webview;

- [IBAction]saveWeb:(id)sender;


@end



////////////////////////////////////////////////////////////////////////////////
//
//  SaveWebViewController.m
//  SaveWeb
//
//  
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "SaveWebViewController.h"

@implementation SaveWebViewController

- (IBAction)saveWeb:(id)sender {

 UIGraphicsBeginImageContext(webView.frame.size);
 [self.view.layer renderInContext: UIGraphicsGetCurrentContext()];
 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
}



// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}



// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}



//Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
 [super viewDidLoad];

 [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

1 个答案:

答案 0 :(得分:4)

-(IBAction)saveWeb:(id)sender 
{

    UIGraphicsBeginImageContext(webview.frame.size);
    [self.webview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil,nil,nil); 

}   ////make sure u add the quartzcore frame work to get rid of the renderincontext problem flag