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

上传人:枫** 文档编号:512040612 上传时间:2023-08-15 格式:DOCX 页数:14 大小:27.57KB
返回 下载 相关 举报
尚学堂iOS开发中最有用关键的代码合集_第1页
第1页 / 共14页
尚学堂iOS开发中最有用关键的代码合集_第2页
第2页 / 共14页
尚学堂iOS开发中最有用关键的代码合集_第3页
第3页 / 共14页
尚学堂iOS开发中最有用关键的代码合集_第4页
第4页 / 共14页
尚学堂iOS开发中最有用关键的代码合集_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《尚学堂iOS开发中最有用关键的代码合集》由会员分享,可在线阅读,更多相关《尚学堂iOS开发中最有用关键的代码合集(14页珍藏版)》请在金锄头文库上搜索。

1、.iOS开发中最有用关键的代码合集本文整理了,在iOS开发中我们所遇到一些开发问题的技巧类的代码,让你在开发过程中避免了很多弯路,希望能给你的开发带来帮助和启发。1.判断格式是否正确的代码:1. /利用正则表达式验证-(BOOL)isValidateEmail:(NSString*)email2. 3. NSString*emailRegex=A-Z0-9a-z._%+-+A-Za-z0-9.-+.A-Za-z2,4;4. NSPredicate*emailTest=NSPredicatepredicateWithFormat:SELFMATCHES%,emailRegex;5. return

2、emailTestevaluateWithObject:email;6. 2.图片压缩1. 用法:UIImage*yourImage=selfimageWithImageSimple:imagescaledToSize:CGSizeMake(210.0,210.0);/压缩图片-(UIImage*)imageWithImageSimple:(UIImage*)imagescaledToSize:(CGSize)newSize2. 3. /CreateagraphicsimagecontextUIGraphicsBeginImageContext(newSize);4. /Telltheoldi

3、magetodrawinthisnewcontext,withthedesired/newsizeimagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height);5. /GetthenewimagefromthecontextUIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();6. /EndthecontextUIGraphicsEndImageContext();7. /Returnthenewimage.returnnewImage;8. 3.亲测可用的图片上传代

4、码1. -(IBAction)uploadButton:(id)sender2. UIImage*image=UIImageimageNamed:1.jpg;/图片名NSData*imageData=UIImageJPEGRepresentation(image,0.5);/压缩比例NSLog(字节数:%i,imageDatalength);3. /posturlNSString*urlString=192.168.1.113:8090/text/UploadServlet;4. /服务器地址/settinguptherequestobjectnowNSMutableURLRequest*re

5、quest=NSMutableURLRequestallocinit;5. requestsetURL:NSURLURLWithString:urlString;6. requestsetHTTPMethod:POST;7. /NSString*boundary=NSStringstringWithString:-9;8. NSString*contentType=NSStringstringWithFormat:multipart/form-data;boundary=%,boundary;9. requestaddValue:contentTypeforHTTPHeaderField:Co

6、ntent-Type;10. /NSMutableData*body=NSMutableDatadata;11. bodyappendData:NSStringstringWithFormat:rn-%rn,boundarydataUsingEncoding:NSUTF8StringEncoding;12. bodyappendData:NSStringstringWithString:Content-Disposition:form-data;name=userfile;filename=2.pngrndataUsingEncoding:NSUTF8StringEncoding;/上传上去的

7、图片名字bodyappendData:NSStringstringWithString:Content-Type:application/octet-streamrnrndataUsingEncoding:NSUTF8StringEncoding;13. bodyappendData:NSDatadataWithData:imageData;14. bodyappendData:NSStringstringWithFormat:rn-%-rn,boundarydataUsingEncoding:NSUTF8StringEncoding;15. requestsetHTTPBody:body;1

8、6. /NSLog(1-body:%,body);NSLog(2-request:%,request);17. NSData*returnData=NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil;18. NSString*returnString=NSStringallocinitWithData:returnDataencoding:NSUTF8StringEncoding;19. NSLog(3-测试输出:%,returnString);4.给imageView加载图片1. UIImag

9、e*myImage=UIImageimageNamed:1.jpg;2. imageViewsetImage:myImage;3. self.viewaddSubview:imageView;5.对图库的操作1. 选择相册:UIImagePickerControllerSourceTypesourceType=UIImagePickerControllerSourceTypeCamera;2. if(!UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera)3. sourceTyp

10、e=UIImagePickerControllerSourceTypePhotoLibrary;4. 5. UIImagePickerController*picker=UIImagePickerControllerallocinit;6. picker.delegate=self;7. picker.allowsEditing=YES;8. picker.sourceType=sourceType;9. selfpresentModalViewController:pickeranimated:YES;10. 选择完毕:-(void)imagePickerController:(UIImag

11、ePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info11. 12. pickerdismissModalViewControllerAnimated:YES;13. UIImage*image=infoobjectForKey:UIImagePickerControllerEditedImage;14. selfperformSelector:selector(selectPic:)withObject:imageafterDelay:0.1;15. 16. -(void)selectPic:(UI

12、Image*)image17. 18. NSLog(image%,image);19. imageView=UIImageViewallocinitWithImage: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为自己定义的方法,编辑选取照片后要实现的效果取

13、消选择:-(void)imagePickerControllerDIdCancel:(UIImagePickerController*)picker25. 26. pickerdismissModalViewControllerAnimated:YES;27. 6.跳到下个View1. nextWebView=WEBViewControllerallocinitWithNibName:WEBViewControllerbundle:nil;2. selfpresentModalViewController:nextWebViewanimated:YES;7.创建一个UIBarButton右边按钮1. UIBarButtonItem*rightButton=UIBarButtonItemallocinitWithTitle:右边style:

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 研究生课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号