`
zhangziyangup
  • 浏览: 1187222 次
文章分类
社区版块
存档分类
最新评论

有用的小代码

 
阅读更多
常用代码整理:


12.判断邮箱格式是否正确的代码:
//利用正则表达式验证
-(BOOL)isValidateEmail:(NSString*)email
{
NSString*emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate*emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES%@",emailRegex];
return[emailTestevaluateWithObject:email];
}
13.图片压缩
用法: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
[imagedrawInRect: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.
returnnewImage;
}
14.亲测可用的图片上传代码
- (IBAction)uploadButton:(id)sender {
UIImage*image = [UIImageimageNamed:@"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 = [[NSMutableURLRequestalloc]init] ;
[requestsetURL:[NSURLURLWithString:urlString]];
[requestsetHTTPMethod:@"POST"];
//
NSString*boundary = [NSStringstringWithString:@"---------------------------14737809831466499882746641449"];
NSString*contentType = [NSStringstringWithFormat:@"multipart/form-data;boundary=%@",boundary];
[requestaddValue:contentTypeforHTTPHeaderField:@"Content-Type"];
//
NSMutableData*body = [NSMutableDatadata];
[bodyappendData:[[NSStringstringWithFormat:@"\r\n--%@\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]];
[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]];
[bodyappendData:[NSDatadataWithData:imageData]];
[bodyappendData:[[NSStringstringWithFormat:@"\r\n--%@--\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]];
[requestsetHTTPBody:body];
// NSLog(@"1-body:%@",body);
NSLog(@"2-request:%@",request);
NSData*returnData = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
NSString*returnString = [[NSStringalloc]initWithData:returnDataencoding:NSUTF8StringEncoding];
NSLog(@"3-测试输出:%@",returnString);
15.imageView加载图片
UIImage*myImage = [UIImageimageNamed:@"1.jpg"];
[imageViewsetImage:myImage];
[self.viewaddSubview:imageView];
16.对图库的操作
选择相册:
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];
}
17.跳到下个View
nextWebView= [[WEBViewControlleralloc]initWithNibName:@"WEBViewController"bundle:nil];
[selfpresentModalViewController:nextWebViewanimated:YES];


//创建一个UIBarButtonItem右边按钮
UIBarButtonItem*rightButton = [[UIBarButtonItemalloc]initWithTitle:@"右边"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(clickRightButton)];
[self.navigationItemsetRightBarButtonItem: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;
30.代码生成button
CGRectframe =CGRectMake(0,400,72.0,37.0);
UIButton*button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.frame= frame;
[buttonsetTitle:@"新添加的按钮"forState:UIControlStateNormal];
button.backgroundColor= [UIColorclearColor];
button.tag=2000;
[buttonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
31.让某个控件在View的中心位置显示:
(某个控件,比如labelViewlabel.center=self.view.center;
32.好看的文字处理
tableViewcelltextLabel为例子:
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;
33.———————-隐藏Status Bar—————————–
读者可能知道一个简易的方法,那就是在程序的viewDidLoad中加入
[[UIApplication sharedApplication]setStatusBarHidden:YES animated:NO];
33.更改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];
34.键盘透明
textField.keyboardAppearance = UIKeyboardAppearanceAlert;

状态栏的网络活动风火轮是否旋转
[UIApplication sharedApplication].networkActivityIndicatorVisible
默认值是NO

35截取屏幕图片
//创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400)
UIGraphicsBeginImageContext(CGSizeMake(200,400));

//renderInContext
呈现接受者及其子范围到指定的上下文
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
//
返回一个基于当前图形上下文的图片
UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
//
移除栈顶的基于当前位图的图形上下文
UIGraphicsEndImageContext();
//
png格式返回指定图片的数据
imageData = UIImagePNGR
epresentation(aImage);
36更改cell选中的背景
UIView *myview = [[UIView alloc] init];
myview.frame = CGRectMake(0, 0, 320, 47);
myview.backgroundColor = [UIColorcolorWithPatternImage:[UIImage imageNamed:@"0006.png"]];
cell.selectedBackgroundView = myview;
37显示图像:
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];
38.能让图片适应框的大小(没有确认)
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];
39.实现点击图片进行跳转的代码:生成一个带有背景图片的button,给button绑定想要的事件!
UIButton*imgButton=[[UIButtonalloc]initWithFrame:CGRectMake(0,0,120,120)];
[imgButtonsetBackgroundImage:(UIImage*)[self.imgArrayobjectAtIndex:indexPath.row]forState:UIControlStateNormal];
imgButton.tag=[indexPathrow];
[imgButtonaddTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
分享到:
评论

相关推荐

    小波去噪MATLAB代码

    本资源包含的是MATLAB代码实现的小波去噪算法,旨在帮助用户理解和应用小波去噪技术。 小波去噪的基本原理是利用小波分析对信号进行多尺度分解,将信号分解为不同频率成分的细节系数。因为噪声通常在高频部分更为...

    200个C语言小游戏源代码

    "200个C语言小游戏源代码"集合提供了丰富的实践项目,旨在帮助学习者通过实际操作来加深对C语言的理解。下面将详细探讨这些小游戏背后涉及的C语言知识点。 1. 基本语法:所有C语言程序都始于`#include`预处理指令,...

    有用代码上传有用有用代码上

    最新劳动法供您下载最新劳动法供您下载最新劳动法供您下载最新劳动法供您下载

    小游戏源代码_小游戏源码_

    【标题】:“小游戏源代码_小游戏源码_”这一标题明确指出我们关注的主题是小游戏的源代码,这通常指的是用于创建简单游戏的编程代码。源代码是程序员编写的游戏设计蓝图,它包含了游戏运行所需的逻辑、规则和算法。...

    气象数据小波分析matlab代码

    这种特性使得小波分析在气象数据处理中特别有用,因为它能够揭示数据在不同时间和空间尺度上的变化模式。 在MATLAB中,执行小波分析通常涉及以下步骤: 1. **选择小波基**:MATLAB提供了多种预定义的小波基,如...

    ios开发有用的小代码集合

    ios开发中常用的小代码集合,工作开发过程中收集,非常适用。

    一些有用的源代码文件...

    标题中的“一些有用的源代码文件”表明这是一份包含可学习或参考的源代码的集合。源代码是程序员用特定编程语言编写的指令,用于控制计算机执行特定任务。这些文件可能是来自不同项目或用途,涵盖了各种功能和算法,...

    matlab 有用的代码大全

    下面,我们将深入探讨这个“matlab 有用的代码大全”中可能包含的知识点。 首先,MATLAB的核心在于它的矩阵运算能力。在描述中提到的“例题全部代码”,可能包含了各种基础和进阶的矩阵操作,如矩阵的创建、索引、...

    小波降噪软硬阈值改进阈值matlab代码.zip

    总结,小波降噪是一种强大的工具,用于从图像中提取有用信息并去除噪声。软硬阈值和改进阈值策略是小波降噪的关键步骤,而提供的MATLAB代码为理解这些方法提供了直观的实例。通过学习和实践这些代码,我们可以深入...

    小波纹理识别 源代码

    在纹理识别中,小波分析的优势在于它能提供尺度和位置的信息,这对于识别具有多种尺度和方向变化的纹理非常有用。 源代码中的"wavelet.m"可能是一个MATLAB脚本,用于执行小波分析和纹理特征提取。MATLAB是一种广泛...

    C语言小游戏源代码

    这个“C语言小游戏源代码”压缩包包含了一系列利用C语言编写的经典小游戏,如赛车、扫雷、坦克和五子棋等,这些都是学习C语言和程序设计的良好实践案例。 首先,让我们逐一探讨这些小游戏所涉及的C语言知识点: 1....

    一个asp的小代码,小网页

    一个asp自作的小代码,对一些初级者有用,但对其他高手,哈哈……

    小工具类-番茄时钟源代码【含图文文档教程+源码导入教程+操作界面截图】

    【小工具类-番茄时钟源代码】是一个微信小程序设计项目开发中的实例,它提供了用于时间管理的番茄工作法的小程序源代码。这个资源包包含了多个组成部分,旨在帮助开发者理解和应用该源代码。 首先,"详细图文文档...

    C语言200个小程序源代码

    "C语言200个小程序源代码"是一个专门为初学者设计的学习资源,它包含了一系列从小到大的C语言程序,旨在帮助学习者逐步掌握C语言的核心概念和编程技巧。 1. **基本语法与结构**:C语言的基础在于它的语法,包括变量...

    微信小程序支付功能源代码

    `支付文档.docx`可能是关于如何集成和使用这些源代码的详细文档,包括配置步骤、注意事项以及可能出现的问题和解决方案,对于初学者尤其有用。 另外,`外卖:实现类似锚点功能.rar`可能是一个与支付无关的示例,它...

    java小游戏实例源代码

    9. **代码结构与设计模式**:源代码可能会展示良好的代码组织方式,如模块化设计,也可能使用到常见的设计模式,如工厂模式、单例模式等,这些对于提高代码可读性和可维护性非常有用。 10. **测试**:尽管可能不...

    一些很实用的C++小代码

    这是一些小的c++代码 对初学者很有用

    几种小游戏的代码 很有用

    对于想开发游戏的人来说很有帮组 有好几种小游戏

    几个有用的代码。。。

    或许对你有用,注意著作权归本人所有!。。。。。。

    代码之美 代码之美 代码之美 代码之美

    函数和类的设计应尽可能小而专注,避免大块的代码段。 3. **可维护性**:优秀的代码需要考虑到未来的需求变化和扩展。模块化设计允许代码各部分独立,易于修改和复用。使用设计模式可以提高代码的可维护性,例如...

Global site tag (gtag.js) - Google Analytics