您的位置:首页 > 移动开发 > IOS开发

ios 开发笔记和技巧总结 (六)

2015-12-03 11:15 507 查看
获取手机号码,和IMEI

获取本地iphone手机号码

[[NSUserDefaults standardUserDefaults] valueForKey:@"SBFormattedPhoneNumber"];

获取手机的imei

#import "Message/NetworkController.h"

NetworkController *ntc=[[NetworkController sharedInstance] autorelease];

NSString *imeistring = [ntc IMEI];

imeistring就是获取的imei。 IMEI(International Mobile Equipment Identity)是国际移动设备身份码的缩写,

国际移动装备辨识码,是由15位数字组成的"电子串号",它与每台手机一一对应,而且该码是全世界唯一的。

iPhone开发笔记 退回输入键盘:自定义颜色:隐藏状态栏:

退回输入键盘:

- (BOOL)
textFieldShouldReturn:(id)textField{

[textField resignFirstResponder];

}

CGRect

CGRect frame = CGRectMake (origin.x, origin.y, size.width, size.height);矩形

NSStringFromCGRect(someCG) 把CGRect结构转变为格式化字符串;

CGRectFromString(aString) 由字符串恢复出矩形;

CGRectInset(aRect) 创建较小或较大的矩形(中心点相同),+较小 -较大

CGRectIntersectsRect(rect1, rect2) 判断两矩形是否交叉,是否重叠

CGRectZero 高度和宽度为零的/位于(0,0)的矩形常量

CGPoint & CGSize

CGPoint aPoint = CGPointMake(x, y); CGSize aSize = CGSizeMake(width, height);

设置透明度 [myView setAlpha:value]; (0.0 < value < 1.0)

设置背景色 [myView setBackgroundColor:[UIColor redColor]];

(blackColor;darkGrayColor;lightGrayColor;whiteColor;grayColor; redColor; greenColor; blueColor; cyanColor;yellowColor;magentaColor;

orangeColor;purpleColor;brownColor;
clearColor; )

自定义颜色:

UIColor *newColor = [[UIColor alloc] initWithRed:(float) green:(float) blue:(float) alpha:(float)];
0.0~1.0

320X480 480X320 状态栏高 20 像素高 导航栏 工具栏 44像素高

隐藏状态栏:

[[UIApplication shareApplication] setStatusBarHidden: YES animated:NO]

横屏:

[[UIApplication shareApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight].

orientation == UIInterfaceOrientationLandscapeLeft

window=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen] bounds];全屏

自动适应父视图大小:

aView.autoresizingSubviews = YES;

aView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

定义按钮

UIButton *scaleUpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[scaleUpButton setTitle:@"放 大" forState:UIControlStateNormal];

scaleUpButton.frame = CGRectMake(40, 420, 100, 40);

[scaleUpButton addTarget:self action:@selector(scaleUp) forControlEvents:UIControlEventTouchUpInside];

设置视图背景图片

UIImageView *aView;

[aView setImage:[UIImage imageNamed:@”name.png”]];

view1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image1.png"]];

UISlider *slider = (UISlider *) sender;

NSString *newText = [[NSString alloc] initWithFormat:@”%d”, (int)(slider.value + 0.5f)];

label.text = newText;

活动表单 <UIActionSheetDelegate>

- (IBActive) someButtonPressed:(id) sender

{

UIActionSheet *actionSheet = [[UIActionSheet alloc]

initWithTitle:@”Are you sure?”

delegate:self

cancelButtonTitle:@”No way!”

destructiveButtonTitle:@”Yes, I’m Sure!”

otherButtonTitles:nil];

[actionSheet showInView:self.view];

}

警告视图 <UIAlertViewDelegate>

- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex

{

if(buttonIndex != [actionSheet cancelButtonIndex])

{

NSString *message = [[NSString alloc] initWithFormat:@”You can

breathe easy, everything went OK.”];

UIAlertView *alert = [[UIAlertView alloc]

initWithTitle:@”Something was done”

message:message

delegate:self

cancelButtonTitle:@”OK”

otherButtonTitles:nil];

[alert show];

}

}

动画效果

-(void)doChange:(id)sender

{

if(view2 == nil)

{

[self loadSec];

}

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:1];

[UIView setAnimationTransition:([view1 superview]?UIViewAnimationTransitionFlipFromLeft:UIViewAnimationTransitionFlipFromRight)forView:self.view
cache:YES];

if([view1 superview]!= nil)

{

[view1 removeFromSuperview];

[self.view addSubview:view2];

}else {

[view2 removeFromSuperview];

[self.view addSubview:view1];

}

[UIView commitAnimations];

}

iphone开发中图像处理四个要点

对于iOS开发者而言,图像处理是一个非常重要的方面。iPhone图像通常存储在以下4个地方:

  相册(PhotoAlums):用户可以使用UIImagePickerController类提供的交互对话框从该相册中获取图像。

  应用程序包:将图像与可执行程序、Info.plist文件和其他资源一同存储,用户可以通过本地文件路径由imageNamed:方法来读取这些基于包的图像。

  沙盒:借助沙盒,可以将文件存储到Documents、Library和tmp文件夹中。

  因特网(Internet):应用程序可通过URL资源从网上下载图像。

  图像文件的位置决定着读取该文件的具体方式。相册中的图片及其路径无法直接从应用程序访问,只有终端用户能够浏览和选择图像,使所选图像对应用程序可用。图像也不能由URL直接初始化。图像源不同读取方式也不同:

  1. 从应用程序包加载图像

  UIImage类提供了一种加载应用程序包中存储的任意图像的简单方法,即通过文件名及其扩展名调用imageNamed:方法。

  myImage = [UIImage imageNamed:@icon.png];

  为避免本地图像缓存及其有效利用内存空间的问题,还可用imageWithContentsOfFile:替换,这个方法会返回从某一具体路径中加载的图像,这个路径需以参数形式提供。当然,若要从应用程序包中获取图像路径,可以查询NSBundle类为给定资源查找路径。示例代码:

NSString *path = [[NSBundle mainBundle] pathForResource:@icon ofType:@png];

myImage = [UIImage imageWithContentsOfFile:path];

  2. 从沙盒加载图像

  默认情况下,每个沙盒包含3个文件夹:Documents、Library和tmp。图像等由应用程序生成的数据通常位于Documents文件夹内。在iPhone开发中可以通过调用实用工具主目录函数可靠地定位顶级沙盒文件夹。通过NSHomeDirectory()返回结果,我们可以向下导航一级到Documents文件夹,完全可以保证正确的位置。示例代码:

NSString *documentsFolder()

  return [NSHomeDirectory()

  stringByAppendingPathComponent:@Documents];

  // 加载图像

  path = [documentsFolder() stringByAppendingPathComponent:@image.png];

  return [UIImage imageWithContentsOfFile:path];

  3. 从URL资源加载图像

  UIImage类可以从NSData实例加载图像,但它不能直接从URL字符串或NSURL对象加载图像。因此,只能为UIImage提供已经从URL下载的数据(即要创建由URL内容初始化的NSData实例)。

  NSURL *url = [NSURL URLWithString:@http://www.cnblogs.com/lovecode/images/demo.jpg];

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url];

  // 类似地我们可以直接创建个类方法

  // 由具体的一个URL字符串,返回通过该资源构建的UIImage

  + (UIImage *) imageFromURLString: (NSString *) urlstring

  return [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlstring]]];

  4. 从相册加载数据

  UIImagePickerController类帮助我们从iPhone相册中选择图像。它提供一个独立的视图控制器,以模态形式呈现视图。该控制器发回的委托消息能够反映用户选择的图像。

  UIImagePickerControllerSourceTypePhotoLibrary 所有同步到iPhone的图像以及包括用户拍摄的图片在内的任何相册。

  UIImagePickerControllerSourceTypeSavedPhotosAlbum 仅含相册。

  UIImagePickerControllerSourceTypeCamera 允许用户使用iPhone内置的摄像头拍照。

  图像拾取器的委托必须遵守两个协议:UINavigationControllerDelegate和UIImagePickerControllerDelegate。在接口中一定要为设置为拾取器委托的对象声明这两个协议。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: