Ios17个常用代码整理

1.判断邮箱格式是否正确的代码
//利用正则表达式验证
-(BOOL)isValidateEmail:(NSString *)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
return [emailTest evaluateWithObject:email];
}
2.图片压缩
用法:UIImage *yourImage= [self imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)];
//压缩图片
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this newcontext, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();
// Return the new image.
return newImage;
}
3.亲测可用的图片上传代码
- (IBAction)uploadButton:(id)sender {
UIImage *image = [UIImage imageNamed:@"1.jpg"]; //图片名
NSData *imageData = UIImageJPEGRepresentation(image,0.5);//压缩比例
NSLog(@"字节数:%i",[imageData length]);
// post url
NSString *urlString = @"http://192.168.1.113:8090/text/UploadServlet";
//服务器地址
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
//
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
//
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition:form-data; name="userfile"; filename="2.png"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; //上传上去的图片名字
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
// NSLog(@"1-body:%@",body);
NSLog(@"2-request:%@",request);
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"3-测试输出:%@",returnString);
4.给imageView加载图片
UIImage *myImage = [UIImage imageNamed:@"1.jpg"];
[imageView setImage:myImage];
[self.view addSubview:imageView];
5.对图库的操作
选择相册:
UIImagePickerControllerSourceTypesourceType=UIImagePickerControllerSourceTypeCamera;
if (![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
}
UIImagePickerController * picker = [[UIImagePickerControlleralloc]init];
picker.delegate = self;
picker.allowsEditing=YES;
picker.sourceType=sourceType;
[self presentModalViewController:picker animated:YES];
选择完毕:
-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
UIImage * image=[info objectForKey:UIImagePickerControllerEditedImage];
[self performSelector:@selector(selectPic:) withObject:imageafterDelay:0.1];
}
-(void)selectPic:(UIImage*)image
{
NSLog(@"image%@",image);
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[self.viewaddSubview:imageView];
[self performSelectorInBackground:@selector(detect:) withObject:nil];
}
detect为自己定义的方法,编辑选取照片后要实现的效果
取消选择:
-(void)imagePickerControllerDIdCancel:(UIImagePickerController*)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
6.跳到下个View
nextWebView = [[WEBViewController alloc] initWithNibName:@"WEBViewController" bundle:nil];
[self presentModalViewController:nextWebView animated:YES];
//创建一个UIBarButtonItem右边按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)];
[self.navigationItem setRightBarButtonItem:rightButton];
设置navigationBar隐藏
self.navigationController.navigationBarHidden = YES;//
iOS开发之UIlabel多行文字自动换行 (自动折行)
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 180)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 150)];
label.text = @"Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Helloworld!";
//背景颜色为红色
label.backgroundColor = [UIColor redColor];
//设置字体颜色为白色
label.textColor = [UIColor whiteColor];
//文字居中显示
label.textAlignment = UITextAlignmentCenter;
//自动折行设置
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
7.代码生成button
CGRect frame = CGRectMake(0, 400, 72.0, 37.0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"新添加的按钮" forState: UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
8.让某个控件在View的中心位置显示
(某个控件,比如label,View)label.center = self.view.center;
9.好看的文字处理
以tableView中cell的textLabel为例子:
cell.backgroundColor = [UIColorscrollViewTexturedBackgroundColor];
//设置文字的字体
cell.textLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:100.0f];
//设置文字的颜色
cell.textLabel.textColor = [UIColor orangeColor];
//设置文字的背景颜色
cell.textLabel.shadowColor = [UIColor whiteColor];
//设置文字的显示位置
cell.textLabel.textAlignment = UITextAlignmentCenter;
10.隐藏Status Bar
读者可能知道一个简易的方法,那就是在程序的viewDidLoad中加入
[[UIApplication sharedApplication]setStatusBarHidden:YES animated:NO];
11.更改AlertView背景
UIAlertView *theAlert = [[[UIAlertViewalloc] initWithTitle:@"Atention"
message: @"I'm a Chinese!"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Okay",nil] autorelease];
[theAlert show];
UIImage *theImage = [UIImageimageNamed:@"loveChina.png"];
theImage = [theImage stretchableImageWithLeftCapWidth:0topCapHeight:0];
CGSize theSize = [theAlert frame].size;
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(5, 5, theSize.width-10, theSize.height-20)];//这个地方的大小要自己调整,以适应alertview的背景颜色的大小。
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
theAlert.layer.contents = (id)[theImage CGImage];
12.键盘透明
textField.keyboardAppearance = UIKeyboardAppearanceAlert;
状态栏的网络活动风火轮是否旋转
[UIApplication sharedApplication].networkActivityIndicatorVisible,默认值是NO。
13.截取屏幕图片
//创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400)
UIGraphicsBeginImageContext(CGSizeMake(200,400));
//renderInContext 呈现接受者及其子范围到指定的上下文
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
//返回一个基于当前图形上下文的图片
UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
//移除栈顶的基于当前位图的图形上下文
UIGraphicsEndImageContext();
//以png格式返回指定图片的数据
imageData = UIImagePNGRepresentation(aImage);
14.更改cell选中的背景
UIView *myview = [[UIView alloc] init];
myview.frame = CGRectMake(0, 0, 320, 47);
myview.backgroundColor = [UIColorcolorWithPatternImage:[UIImage imageNamed:@"0006.png"]];
cell.selectedBackgroundView = myview;
15.显示图像
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES; //opaque是否透明
[self.view addSubview:myImage];
16.能让图片适应框的大小(没有确认)
NSString*imagePath = [[NSBundle mainBundle] pathForResource:@"XcodeCrash"ofType:@"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:imagePath];
UIImage *newImage= [image transformWidth:80.f height:240.f];
UIImageView *imageView = [[UIImageView alloc]initWithImage:newImage];
[newImagerelease];
[image release];
[self.view addSubview:imageView];
17.实现点击图片进行跳转的代码:生成一个带有背景图片的button,给button绑定想要的事件!
UIButton *imgButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 120, 120)];
[imgButton setBackgroundImage:(UIImage *)[self.imgArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
imgButton.tag=[indexPath row];
[imgButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

 

转载于:https://www.cnblogs.com/jiackyan/p/3522386.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/273877.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ubuntu18 激活 pycharm

1、到官网上下载好对应的版本 2、到安装好的pycharm的bin文件夹下,找到 pycharm.vmoptions 和 pycharm64.vmoptions,在两个文件后面添加代码: -javaagent:-javaagent:/home/maxzhang/user/pycharm/bin/JetbrainsCrack-release-enc.jar&#…

jquery背景自动切换特效

查看效果网址&#xff1a;http://keleyi.com/a/bjad/4kwkql05.htm 本特效的jquery版本只支持1.9.0以下。代码如下&#xff1a; 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2…

pandas和spark的区别

参考&#xff1a;https://blog.csdn.net/u013613428/article/details/78138857

Android ImageView图片自适应

网络上下载下来的图片自适应&#xff1a;android:adjustViewBounds"true"&#xff08;其详细解释在下面&#xff09;<ImageViewandroid:id"id/dynamic_item_image"android:layout_width"wrap_content"android:layout_height"wrap_conten…

Python之IO编程——文件读写、StringIO/BytesIO、操作文件和目录、序列化

BytesIO StringIO操作的只能是str&#xff0c;如果要操作二进制数据&#xff0c;就需要使用BytesIO。BytesIO实现了在内存中读写bytes&#xff0c;我们创建一个BytesIO&#xff0c;然后写入一些bytes&#xff1a; 写入的不是str&#xff0c;而是经过UTF-8编码的bytes。 (1).参考…

都江堰很美-佩服古人_Crmhf的一天

地震遗迹&#xff1a;一条背街&#xff0c;损坏严重&#xff0c;基本没什么人。真正的水利工程&#xff0c;值得每个人学习&#xff1a;转载于:https://www.cnblogs.com/crmhf/p/3823157.html

爬虫的增量式抓取和数据更新

不管是产生新页面&#xff0c;还是原本的页面更新&#xff0c;这种变化都被称为增量&#xff0c; 而爬取过程则被称为增量爬取。那如何进行增量式的爬取工作呢&#xff1f;回想一下爬虫的工作流程&#xff1a; 发送URL请求 ----- 获得响应 ----- 解析内容 ----- 存储内容 我们…

Spring Data JPA初使用 *****重要********

Spring Data JPA初使用我们都知道Spring是一个非常优秀的JavaEE整合框架&#xff0c;它尽可能的减少我们开发的工作量和难度。在持久层的业务逻辑方面&#xff0c;Spring开源组织又给我们带来了同样优秀的Spring Data JPA。通常我们写持久层&#xff0c;都是先写一个接口&#…

flask-笔记

-super() 使用super()保留基模板中定义的原始内容 - link标签&#xff1a; 用来指定当前文档和外部资源的关系。它最常见的是用来链接样式表&#xff0c;也用来创建网站图标(既是网站图标样式也包括移动设备和app图标)。 -csrf: CSRF概念&#xff1a;CSRF跨站点请求伪造(…

MySQL 无法连接

Host localhost is not allowed to connect to this MySQL server 错误 解决办法&#xff1a; C:\Program Files\MySQL\MySQL Server 5.5\my.ini 在[mysqld]下加下面两行&#xff0c; skip-name-resolve skip-grant-tables 重启mysql的windows服务&#xff0c;在mysql命令行界面…

能让你少写1000行代码的20个正则表达式

参考: (1).http://www.codeceo.com/article/20-regular-expressions.html

http请求中的Query String Parameters、Form Data、Request Payload

参考: (1).(http请求参数之Query String Parameters、Form Data、Request Payload) - https://www.jianshu.com/p/c81ec1a547ad

蜜罐

http://www.projecthoneypot.org/home.php转载于:https://www.cnblogs.com/diyunpeng/p/3534507.html

php中json_decode返回数组或对象的实例

1.json_decode() json_decode (PHP 5 > 5.2.0, PECL json > 1.2.0) json_decode — 对 JSON 格式的字符串进行编码 说明 mixed json_decode ( string $json [, bool $assoc ] ) 接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 参数 json 待解码的 json string 格式的…

如何精通js

参考: (1.)https://www.zhihu.com/search?typecontent&q%E5%A6%82%E4%BD%95%E7%B2%BE%E9%80%9Ajs

程序员怎么样才能进入微软?

程序员怎么样才能进入微软&#xff1f; 程序员到微软中国总裁 “打工皇帝”长沙晒成功之道 程序员面试之道之走进微软 应该是西北大学的学生&#xff0c;距离我好近&#xff08;我也在西安&#xff09;&#xff0c;可是又好远&#xff08;人家拿到了MS的offer&#xff09;。 专…

python中的装饰器-(重复阅读)

---1--- 假设我们要增强某个函数的功能&#xff0c;比如&#xff0c;在函数调用前后自动打印日志&#xff0c;但又不希望修改某个函数的定义&#xff0c;这种在代码运行期间动态增加功能的方式&#xff0c;称之为“装饰器”&#xff08;Decorator). 装饰器本质上是一个Python…

[转帖]好技术领导,差技术领导

团队合作一个优秀的技术领导必然是团队的一份子&#xff0c;他们认为当整个团队成功时自己才称得上成功。他们不仅要做好繁杂和不讨好的本职工作&#xff0c;还要清除项目中的障碍&#xff0c;从而让整个团队能够以100%的效率运转起来。一个好的技术领导会努力拓宽团队在技术上…

python有哪些常用的库

参考: (1).https://www.zhihu.com/question/20501628/answer/19542741(Python 常用的标准库以及第三方库有哪些&#xff1f;)

C#打开文件对话框和文件夹对话框

打开文件对话框OpenFileDialog OpenFileDialog ofd new OpenFileDialog();ofd.Filter "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件|*.*";ofd.ValidateNames true;ofd.CheckPathExists true;ofd.CheckFileExists true;if (ofd.ShowDialog() DialogResult.O…