iOS菜鸟食谱

相册相机以及Transform

2016-03-19  本文已影响38人  每日总结

相册相机

需要遵守两个协议 UINavigationControllerDelegate,UIImagePickerControllerDelegate

@interface RootViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@end
@implementation RootViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cameraButton.frame = CGRectMake(100,100,100,100);
    cameraButton.backgroundColor = [UIColor redColor];
    [cameraButton addTarget:self action:@selector(cameraAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cameraButton];
    UIButton *photoButton = [UIButton buttonWithType:UIButtonTypeSystem];
    photoButton.frame = CGRectMake(100,200,100,100);
    photoButton.backgroundColor = [UIColor blueColor];
    [photoButton addTarget:self action:@selector(photoAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:photoButton];
}
- (void)cameraAction:(UIButton *)button{
    // 判断有没有相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //创建相机对象
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        //设置调用的是相机还是相册
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        //是否可以编辑
        picker.allowEditing = YES;
        //设置代理
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
        [picker release];
    }
}
- (void)photoAction:(UIButton *)button{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
        [picker release];
    }
}
// 实现协议中的方法
//选完照片 或者 拍摄完成后 调用的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(200,100,200,200)];
    imgView.image = [info objectForKey:UIImagePickerControllerOriginalImage];//info字典里的key值可以用NSLog打印出来,OriginalImage是原始图片;
    [self addSubview:imgView];
    [imgView release];
    [self dismissViewControllerAnimated:YES completion:nil];// 这里dismiss的是弹出来的相册界面;
}

Transform

transform 本质是一个3*3的矩阵,通过改变其中的元素可以达到对UIView的缩放,平移,旋转的效果,但实际上UIView的frame却不会改变;

                                               [a  b  0
                                                c  d  0    * [x1 y1 1]
                                                tx ty 1]
                              x2 = a*x1 + c*y1 + tx;
                              y2 = b*x1 + d*y1 + ty;
//CGAffineTranform方法对view的Transform属性的改变可以用以上两个矩阵相乘的结果来表示

struct CGAffineTransform {

    CGFloat a, b, c, d;// 这四个参数是用于控制transform属性的旋转和缩放
    CGFloat tx, ty; // 这两个参数是用于控制transform属性的平移
};
************************************************
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100,100,100,100)];
[self.view addSubview:view];
[view release];
view.transform = CGAffineTransformMakeTranslation(tx, ty);//返回矩阵[1 0 0 1 tx ty];
//相乘结果是
//x2 = x1 + tx; 在X轴移动tx个单位
//y2 = y1 + ty;  在Y轴移动ty个单位
view.transform = CGAffineTransformMakeScale(sx ,sy);// 返回矩阵[sx 0 0 sy 0 0];
//相乘结果是
//x2 = x1*sx; X轴放大(缩小)为原来的sx倍
//y2 = y1*sy; Y轴放大(缩小)为原来的sy倍
view.transform = CGAffineTransformMakeRotation(angle);// 返回矩阵[cos(angle) sin(angle) -sin(angle) cos(angle) 0 0];
//相乘结果是
//x2 = x1*cos(angle)+y1*sin(angle)
//y2 = x1*-sin(angle)+y1*cos(angle)
//旋转angle度;
*******************************

除了上面三种外,还有三种是方法中没有Make的,在原有参数的前面加上一个CGAffineTransform类型的参数t,返回的是在t的基础上进行对应的变化后的矩阵

这些方法在变化完成后都需要把相关对象的对应属性置为初值,否则下一次变化开始view会恢复原状,如
pinch手势的pinch.scale要置为1
rotation手势的rotation.rotation要置为0
pan手势的[pan setTranslation:CGPointZero inView:pan.view];将现在的位置置为0点;
参考资料及详细说明
http://www.cnblogs.com/smileEvday/archive/2013/04/23/Rotate1.html
博主:一片枫叶

上一篇下一篇

猜你喜欢

热点阅读