IOS开发基础(基于Objective C)

1.关于UI

获取iphone宽和高

//可以将屏幕大小定义成一个宏
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
//宽度
int w = [[UIScreen mainScreen] bounds].size.width
//高度
int h=[[UIScreen mainScreen] bounds].size.height

2.UIButton

//初始化
UIBUtton* _btnInit = [[UIButton alloc] init];
//设置位置及控件的宽高
_btnInit.frame = CGRectMake(30, 30, 100, 20);
//标题
[_btnInit setTitle:@"初始化" forState:UIControlStateNormal];
//标题颜色
[_btnInit setTitleColor:UIColor.blueColor forState:normal];
//字体尺寸
_btnInit.titleLabel.font = [UIFont systemFontOfSize: 18.0];
//背景颜色
_btnInit.backgroundColor = [UIColor blueColor];
//边距padding 上左下右
_btnInit.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 10);
//点击事件
[_btnInit addTarget:self action:@selector(CameraSwitch:) forControlEvents:UIControlEventTouchUpInside];
//添加到父view
[self.view addSubview:_btnInit];

点击事件

- (void)CameraSwitch:(UIButton *)btn{
    [self.videoCapture switchCameraPosition:glView drawView:drawView];
}

3.UIImage

几种加载UIImage方式

    // 1.使用imageNamed
    UIImage *image1 = [UIImage imageNamed:@"1.jpg"];
    // 2.使用imageWithContentsOfFile
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
    UIImage *image2 = [UIImage imageWithContentsOfFile:path];
    // 3.使用initWithContentsOfFile
    UIImage *image3 = [[UIImage alloc] initWithContentsOfFile:path];
    // 4.使用imageWithData
    NSData *imageData = [NSData dataWithContentsOfFile:path];
    UIImage *image4 = [UIImage imageWithData:imageData];
    /* ----- 设置UIImageView属性 ----- */
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    // 指定imageView的image对象
    imageView.image = image1;
    // 设置图片内容的布局方式
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    // 设置imageView的背景
    imageView.backgroundColor = [UIColor whiteColor];
    // 设置imageView透明度
    imageView.alpha = 1.0;
    // 添加viewDidLoad到self.view
    [self.view addSubview:imageView];
    /* ----- 加载动态图 ----- */
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"];
    NSString *path3 = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"jpg"];
    imageView.animationImages = @[[UIImage imageWithContentsOfFile:path1],[UIImage imageWithContentsOfFile:path2],[UIImage imageWithContentsOfFile:path3]];
    imageView.animationDuration = 5.0f;     // 设置循环一次的时间
    imageView.animationRepeatCount = 0;     // 设置循环次数(0为无线循环)
    [imageView startAnimating];             // 开始动画
//    [imageView stopAnimating];              // 停止动画

图片点击事件

UIImageView* imageView=[[UIImageView alloc]init];
imageView.frame = CGRectMake(100, 160, 200, 200);
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
//目标Image
imageView.image =[UIImage imageNamed:@"0"];
[self.view addSubview:imageView];
//图片点击事件,clickImage为点击方法
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage)];
[imageView addGestureRecognizer:tapGesture];//图片实例添加手势
imageView.userInteractionEnabled = YES;//默认NO不可点击

保存图片到沙盒

- (void)saveImage:(UIImage *)image name:(NSString*)name {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:
                          [NSString stringWithFormat:@"%@", name]];  // 保存文件的名称
 //NSString *DocumentsPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
 //NSString *filePath=[DocumentsPath stringByAppendingString:@"/file.yuv"];
    BOOL result =[UIImagePNGRepresentation(image)writeToFile:filePath   atomically:YES]; // 保存成功会返回YES
    if (result == YES) {
        NSLog(@"保存成功");
    }  
}

下载到Mac: Window—Devices And Simulators—选中app—点击形似设置的按钮—Download Container选择下载路径即可

取出保存的图片

- (void)getImage {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    //图片路径
    NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:
  	                     [NSString stringWithFormat:@"demo.png"]];
    // 文件的名称
    UIImage *img = [UIImage imageWithContentsOfFile:filePath];
    NSLog(@"=== %@", img);
}

待补充。。。

你可能感兴趣的:(IOS,ios)