尚学堂iOS开发中最有用关键的代码合集

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

iOS开发中最有用关键的代码合集本文整理了,在iOS开发中我们所遇到一些开发问题的技巧类的代码,让你在开发过程中避免了很多弯路,希望能给你的开发带来帮助和启发。1.判断邮箱格式是否正确的代码:1.//利用正则表达式验证-(BOOL)isValidateEmail:(NSString*)email2.{3.NSString*emailRegex=@[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4};4.NSPredicate*emailTest=[NSPredicatepredicateWithFormat:@SELFMATCHES%@,emailRegex];5.return[emailTestevaluateWithObject:email];6.}2.图片压缩1.用法:UIImage*yourImage=[selfimageWithImageSimple:imagescaledToSize:CGSizeMake(210.0,210.0)];//压缩图片-(UIImage*)imageWithImageSimple:(UIImage*)imagescaledToSize:(CGSize)newSize2.{3.//CreateagraphicsimagecontextUIGraphicsBeginImageContext(newSize);4.//Telltheoldimagetodrawinthisnewcontext,withthedesired//newsize[imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];5.//GetthenewimagefromthecontextUIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();6.//EndthecontextUIGraphicsEndImageContext();7.//Returnthenewimage.returnnewImage;8.}9.3.亲测可用的图片上传代码1.-(IBAction)uploadButton:(id)sender{2.UIImage*image=[UIImageimageNamed:@1.jpg];//图片名NSData*imageData=UIImageJPEGRepresentation(image,0.5);//压缩比例NSLog(@字节数:%i,[imageDatalength]);3.//posturlNSString*urlString=@服务器地址//settinguptherequestobjectnowNSMutableURLRequest*request=[[NSMutableURLRequestalloc]init];5.[requestsetURL:[NSURLURLWithString:urlString]];6.[requestsetHTTPMethod:@POST];7.//NSString*boundary=[NSStringstringWithString:@---------------------------14737809831466499882746641449];8.NSString*contentType=[NSStringstringWithFormat:@multipart/form-data;boundary=%@,boundary];9.[requestaddValue:contentTypeforHTTPHeaderField:@Content-Type];10.//NSMutableData*body=[NSMutableDatadata];11.[bodyappendData:[[NSStringstringWithFormat:@\r\n--%@\r\n,boundary]dataUsingEncoding:NSUTF8StringEncoding]];12.[bodyappendData:[[NSStringstringWithString:@Content-Disposition:form-data;name=\userfile\;filename=\2.png\\r\n]dataUsingEncoding:NSUTF8StringEncoding]];//上传上去的图片名字[bodyappendData:[[NSStringstringWithString:@Content-Type:application/octet-stream\r\n\r\n]dataUsingEncoding:NSUTF8StringEncoding]];13.[bodyappendData:[NSDatadataWithData:imageData]];14.[bodyappendData:[[NSStringstringWithFormat:@\r\n--%@--\r\n,boundary]dataUsingEncoding:NSUTF8StringEncoding]];15.[requestsetHTTPBody:body];16.//NSLog(@1-body:%@,body);NSLog(@2-request:%@,request);17.NSData*returnData=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];18.NSString*returnString=[[NSStringalloc]initWithData:returnDataencoding:NSUTF8StringEncoding];19.NSLog(@3-测试输出:%@,returnString);4.给imageView加载图片1.UIImage*myImage=[UIImageimageNamed:@1.jpg];2.[imageViewsetImage:myImage];3.[self.viewaddSubview:imageView];5.对图库的操作1.选择相册:UIImagePickerControllerSourceTypesourceType=UIImagePickerControllerSourceTypeCamera;2.if(![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){3.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;4.}5.UIImagePickerController*picker=[[UIImagePickerControlleralloc]init];6.picker.delegate=self;7.picker.allowsEditing=YES;8.picker.sourceType=sourceType;9.[selfpresentModalViewController:pickeranimated:YES];10.选择完毕:-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info11.{12.[pickerdismissModalViewControllerAnimated:YES];13.UIImage*image=[infoobjectForKey:UIImagePickerControllerEditedImage];14.[selfperformSelector:@selector(selectPic:)withObject:imageafterDelay:0.1];15.}16.-(void)selectPic:(UIImage*)image17.{18.NSLog(@image%@,image);19.imageView=[[UIImageViewalloc]initWithImage:image];20.imageView.frame=CGRectMake(0,0,image.size.width,image.size.height);21.[self.viewaddSubview:imageView];22.[selfperformSelectorInBackground:@selector(detect:)withObject:nil];23.}24.detect为自己定义的方法,编辑选取照片后要实现的效果取消选择:-(void)imagePickerControllerDIdCancel:(UIImagePickerController*)picker25.26.{27.[pickerdismissModalViewControllerAnimated:YES];28.}6.跳到下个View1.nextWebView=[[WEBViewControlleralloc]initWithNibName:@WEBViewControllerbundle:nil];2.[selfpresentModalViewController:nextWebViewanimated:YES];7.创建一个UIBarButton右边按钮1.UIBarButtonItem*rightButton=[[UIBarButtonItemalloc]initWithTitle:@右边style:UIBarButtonItemStyleDonetarget:selfaction:@selector(clickRightButton)];2.[self.navigationItemsetRightBarButtonItem:rightButton];8.设置navigationBar隐藏1.self.navigationController.navigationBarHidden=YES;//9.UIlabel多行文字自动换行(自动折行)1.UIView*footerView=[[UIViewalloc]initWithFrame:CGRectMake(10,100,300,180)];UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(10,100,300,150)];label.text=@Helloworld!Helloworld!Helloworld!Helloworld!Helloworld!Helloworld!Helloworld!Helloworld!Helloworld!Helloworld!Helloworld!Helloworld!Helloworld!Helloworld!;//背景颜色为红色label.backgroundColor=[UIColorredColor];//设置字体颜色为白色label.textColor=[UIColorwhiteColor];//文字居中显示label.textAlignment=UITextAlignmentCenter;//自动折行设置label.lineBreakMode=UILineBreakModeWordWrap;label.numberOfLines=0;10.代码生成Button1.CGRectframe=CGRectMake(0,400,72.0,37.0);2.UIButton*butt

1 / 16
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功