UIWebView使用 保存图片的本地,以及设置
来源:互联网 发布:java comp env jdbc 编辑:程序博客网 时间:2024/05/21 22:25
@interface DetailsViewController ()<UIGestureRecognizerDelegate,UIActionSheetDelegate,UIWebViewDelegate> {
NSString *strUrl; //图片网址
int lastContentOffset; //最后位置
NSString *strSize;
int labelSize ;
int tempY; //设置视图的y轴
DetailButtonView *detailButtonView;
}
@property (strong,nonatomic) UIWebView *webView;
@property (strong,nonatomic) LoadreImageView *imgView;//保存网络图片到本地相册
@property (strong,nonatomic) UIView *whiteBackground;//白色覆盖背景
@property (strong,nonatomic) UIView *viewSetting; //设置视图
@property (strong,nonatomic) UIButton *btnGrayView; //灰色覆盖层
@property (strong,nonatomic) UISegmentedControl *sengment;//字体选择器;
@end
staticBOOL openImage = YES;//判断是否打开图片状态
@implementation DetailsViewController
- (void)viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];
labelSize = (int)[defaultsintegerForKey:@"selectionRow"];
NSString *str = [selflaelSize:labelSize];
self.tabBarController.tabBar.hidden =NO;
self.navigationController.navigationBarHidden =YES;
strSize = [NSStringstringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%@'",str];
self.sengment.selectedSegmentIndex =labelSize;
}
- (void)viewDidLoad {
self.imgView = [LoadreImageViewnew];
self.whiteBackground = [[UIViewalloc]initWithFrame:CGRectMake(0,20, SCREEN_WIDTH, SCREEN_HEIGHT - 20)];
self.whiteBackground.backgroundColor = [UIColorwhiteColor];
self.whiteBackground.hidden =YES;
[self.viewaddSubview:self.whiteBackground];
//状态栏目背景颜色
UIView *statusBarView=[[UIViewalloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,20)];
statusBarView.backgroundColor=[UIColorblackColor];
[self.viewaddSubview:statusBarView];
[superviewDidLoad];
self.view.backgroundColor=[UIColorwhiteColor];
// Do any additional setup after loading the view.
//创建webView
self.webView = [UIWebViewnew];
self.webView.frame =CGRectMake(0,20, self.view.frame.size.width,self.view.frame.size.height -60);
self.webView.delegate =self;
self.webView.backgroundColor = [UIColorwhiteColor];
[self.viewaddSubview:self.webView];
//读取html文件
NSString *filePath = [[NSBundlemainBundle]pathForResource:@"1"ofType:@"html"];
NSString *htmlString = [NSStringstringWithContentsOfFile:filePath encoding:NSUTF8StringEncodingerror:nil];
[self.webViewloadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]];
//,弹出界面设置view的时候 显示灰色的view,点击时,回收设置view
self.btnGrayView = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
self.btnGrayView.frame =self.view.bounds;
self.btnGrayView.backgroundColor = [UIColorgrayColor];
self.btnGrayView.alpha =0.3;
[self.btnGrayViewaddTarget:selfaction:@selector(btnGrayViewAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:self.btnGrayView];
self.btnGrayView.hidden =YES;
//设置视图
[selfsettingView];
//创建底部状态栏
detailButtonView = [[DetailButtonViewalloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT -40, self.view.frame.size.width,40)]; //160;
[detailButtonView.btnMoreaddTarget:selfaction:@selector(btnMoreAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:detailButtonView];
//NSString *fontFamily = @"STHeiti";//改字体
NSString *fontFamily = FONT_NAME2;
float fontSize = 16;
UIColor *fontColor = [UIColorblackColor];
NSString *jsString = [NSStringstringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-size: %f; font-family: \"%@\"; color: %@;}\n"
"</style> \n"
"</head> \n"
"<body>%@</body> \n"
"</html>", fontSize, fontFamily, fontColor, htmlString];
[self.webViewloadHTMLString:jsString baseURL:nil];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(doubleTap:)];
//doubleTap.numberOfTouchesRequired = 2;
doubleTap.numberOfTapsRequired =1;
doubleTap.delegate =self;
doubleTap.cancelsTouchesInView =NO;
[self.webViewaddGestureRecognizer:doubleTap];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longGesture:)];
[self.viewaddGestureRecognizer:longPress];
//活动指示器开始
//[[LoadingProgressView getInstance] show];
}
- (void)btnGrayViewAction:(UIButton *)sender {
[UIViewanimateWithDuration:0.5animations:^{
self.viewSetting.center =CGPointMake(SCREEN_WIDTH/2,(SCREEN_HEIGHT -40) + 60);
}];
self.btnGrayView.hidden =YES;
}
- (void)btnMoreAction:(UIButton *)sender {
if (tempY ==SCREEN_HEIGHT- 40) {
tempY = SCREEN_HEIGHT -160;
self.btnGrayView.hidden =NO;
[UIViewanimateWithDuration:0.3animations:^{
detailButtonView.btnMore.transform = CGAffineTransformMakeRotation(M_PI);
}];
}else {
tempY = SCREEN_HEIGHT -40;
self.btnGrayView.hidden =YES;
[UIViewanimateWithDuration:0.3animations:^{
detailButtonView.btnMore.transform = CGAffineTransformMakeRotation(0);
}];
}
[UIViewanimateWithDuration:0.5animations:^{
self.viewSetting.center =CGPointMake(SCREEN_WIDTH/2,tempY +60);
}];
}
- (void)segmentAction:(UISegmentedControl *)seg {
int index = (int)seg.selectedSegmentIndex;
NSString *str = [selflaelSize:index];
NSString *strSize1 = [NSStringstringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%@'",str];
[self.webViewstringByEvaluatingJavaScriptFromString:strSize1];
//保存选中的行数
NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];
[defaultssetInteger: index forKey:@"selectionRow"];
[defaultssynchronize];
}
- (NSString *)laelSize:(int)index {
NSString *str;
switch (index) {
case 0:
str =@"100%";
break;
case 1:
str =@"125%";
break;
case 2:
str =@"150%";
break;
case 3:
str =@"175%";
break;
default:
break;
}
return str;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.webViewstringByEvaluatingJavaScriptFromString:strSize];
//[webView stringByEvaluatingJavaScriptFromString:strxx];
//NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
//NSLog(@"curretURL=%@=",currentURL);
//NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f;document.body.style.color=%@",25,fontColor];
//活动指示器隐藏
//[[LoadingProgressView getInstance] hidde];
//拦截网页图片 并修改图片大小
int imgWidth = self.view.frame.size.width -30;
NSString *strImage = [NSStringstringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth=%d;"
"for(i=0;i <document.images.length;i++){"
"myimg = document.images[i];"
"if(myimg.width > maxwidth){"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"myimg.height = myimg.height * (maxwidth/oldwidth);"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);",imgWidth];
[webView stringByEvaluatingJavaScriptFromString:
strImage];
[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
/*/字体颜色
NSString *fontColor = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '%@'",strFontColor];
[webView stringByEvaluatingJavaScriptFromString:fontColor];
//页面背景色 #2E2E2E
NSString *viewBackground = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.background='%@'",strBackground];
[webView stringByEvaluatingJavaScriptFromString:viewBackground];*/
//NSLog(@"model====XXX=%@=%@=",strFontColor,strBackground);
//NSString *fontFamily = @"STHeiti"; //// Helvetica-Bold Arial @"STHeiti"
//NSString *fontFamily = FONT_NAME2;
//字体
// NSString *str =[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.WebkitFontFamily='Helvetica-Bold'"];//font-family
// [webView stringByEvaluatingJavaScriptFromString:str];
}
//点击获取图片
-(void)doubleTap:(UITapGestureRecognizer *)sender
{
if (openImage ==YES) {
CGPoint pt = [sender locationInView:self.webView];
pt.x =SCREEN_WIDTH/2;
NSString *imgURL = [NSStringstringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];
NSString *urlToSave = [self.webViewstringByEvaluatingJavaScriptFromString:imgURL];
// NSLog(@"image url=%@", urlToSave);
strUrl = urlToSave;
if (urlToSave.length >0) {
[selfshowImageURL:urlToSave point:pt];
[self.imgViewloadImage:urlToSave];
}
}
}
//呈现图片
-(void)showImageURL:(NSString *)url point:(CGPoint)point {
LoadreImageView *showView = [[LoadreImageViewalloc] initWithFrame:CGRectMake(0,0, 320, 270)];
showView.center = point;
[UIViewanimateWithDuration:0.5fanimations:^{
CGPoint newPoint = self.view.center;
//newPoint.y += 10;
showView.center = newPoint;
}];
showView.backgroundColor = [UIColorblackColor];
showView.userInteractionEnabled =YES;
[showViewloadImage:strUrl];
self.whiteBackground.hidden =NO;
self.whiteBackground.userInteractionEnabled =NO;
[self.viewaddSubview:self.whiteBackground];
[self.viewaddSubview:showView];
openImage = NO;
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(handleSingleViewTap:)];
[showViewaddGestureRecognizer:singleTap];
//[self.navigationController setNavigationBarHidden:YES animated:YES];
}
//移除图片查看视图
-(void)handleSingleViewTap:(UITapGestureRecognizer *)sender
{
for (id objin self.view.subviews) {
if ([obj isKindOfClass:[LoadreImageViewclass]]) {
[objremoveFromSuperview];
}
}
openImage = YES;
self.whiteBackground.hidden =YES;
//[self.navigationController setNavigationBarHidden:NO animated:YES];
}
//长按编辑状态
-(void)longGesture:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state ==UIGestureRecognizerStateBegan) {
UIActionSheet *sheet = [[UIActionSheetalloc]initWithTitle:@"保存图片"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"确定",@"取消",nil];
sheet.backgroundColor = [UIColorblackColor];
[sheetshowInView:self.view];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
UIImageWriteToSavedPhotosAlbum(self.imgView.image,self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
break;
case 1:
break;
default:
break;
}
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
NSString *message;
if (!error) {
message =@"成功保存到相册";
}else {
message = [errordescription];
}
NSLog(@"message is %@",message);
}
- (void)settingView {
tempY =SCREEN_HEIGHT - 40;
self.viewSetting = [[UIViewalloc]initWithFrame:CGRectMake(0,tempY, SCREEN_WIDTH, 120)];
self.viewSetting.backgroundColor = [UIColorwhiteColor];
[self.viewaddSubview:self.viewSetting];
//屏幕亮度
UILabel *lblBrightness = [[UILabelalloc]initWithFrame:CGRectMake(10,13, 70, 30)];
lblBrightness.font = [UIFontfontWithName:FONT_NAME1size:FONT_SIZE2];
lblBrightness.text =@"屏幕亮度";
[self.viewSettingaddSubview:lblBrightness];
UISlider *slider = [[UISlideralloc]initWithFrame:CGRectMake(80,22, 210, 20)];
slider.tintColor =BACKER_COLOR;
slider.value =0.30;/*
slider.backgroundColor = [UIColor clearColor];
[slider setMinimumTrackImage:[UIImage imageNamed:@"max.png"] forState:UIControlStateNormal];
[slider setMaximumTrackImage:[UIImage imageNamed:@"min.png"] forState:UIControlStateNormal];
*/
[slider addTarget:selfaction:@selector(sliderAction:)forControlEvents:UIControlEventValueChanged];
[self.viewSettingaddSubview:slider];
//分割线
UIView *viewLine = [[UIViewalloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH ,0.5)];
viewLine.backgroundColor = [UIColorgrayColor];
[self.viewSettingaddSubview:viewLine];
UIView *viewLine2 = [[UIViewalloc]initWithFrame:CGRectMake(10,60, SCREEN_WIDTH -20, 0.5)];
viewLine2.backgroundColor = [UIColorgrayColor];
viewLine2.alpha =0.5;
[self.viewSettingaddSubview:viewLine2];
//字体大小
UILabel *lblSize = [[UILabelalloc]initWithFrame:CGRectMake(10,75, 70, 30)];
lblSize.text =@"字体大小";
lblSize.font = [UIFontfontWithName:FONT_NAME1size:FONT_SIZE2];
[self.viewSettingaddSubview:lblSize];
NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"小字体",@"中字体",@"大字体",@"特大字",nil];
self.sengment = [[UISegmentedControlalloc]initWithItems:segmentedArray];
self.sengment.frame =CGRectMake(80,75, 210, 30);
self.sengment.tintColor = BACKER_COLOR;
self.sengment.selectedSegmentIndex =labelSize;
[self.sengmentaddTarget:selfaction:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];
[self.viewSettingaddSubview:self.sengment];
}
/*
*控制屏幕亮暗度
*/
- (void)sliderAction:(UISlider *)sender {
[[UIScreenmainScreen] setBrightness:sender.value];
}
/*
*现在的位置
*/
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
lastContentOffset = scrollView.contentOffset.y;
}
-(void)swipeLeftRight:(UISwipeGestureRecognizer *)recognizer {
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
@end
- UIWebView使用 保存图片的本地,以及设置
- ios uiwebview 长按图片,保存到本地相册
- ios uiwebview 长按图片,保存到本地相册
- ios uiwebview 长按图片,保存到本地相册
- UIWebView中显示加载本地的图片
- UIWebView显示本地图片的方法
- UIWebView显示本地图片的方法
- UIWebView保存图片
- UIWebView保存图片
- uiwebview保存图片
- UIWebView保存图片
- UIWebView保存图片
- 使用jdbcTemplate 保存图片 至数据库 以及 从数据库读取 保存到本地
- UIWebView加载本地图片
- 点击图片视图缩放以及保存本地
- UIWebView使用本地资源
- UIWebView使用本地资源
- 把UIWebView内容保存图片
- Eclipse SVN 修改用户名和密码
- "undefined reference to" 问题解决方法
- /Z7、/Zi、/ZI(调试信息格式)
- 一行代码,浏览器变临时编辑器
- Android获取运营商信息
- UIWebView使用 保存图片的本地,以及设置
- js验证input输入框(字母,数字,符号,中文)
- mysqli扩展库操作mysql数据库 multi_querry一次性查询并显示
- explicit 与 构造函数 (防止隐式转换)
- 关于sizeof的一切
- POJ 3268 Silver Cow Party 最短路
- 指数平滑算法
- UVA Minimal coverage (贪心)
- spark 数组