相冊相機以及Transform

相冊相機

需要遵守兩個協(xié)議 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]) {
        //創(chuàng)建相機對象
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        //設(shè)置調(diào)用的是相機還是相冊
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        //是否可以編輯
        picker.allowEditing = YES;
        //設(shè)置代理
        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];
    }
}
// 實現(xiàn)協(xié)議中的方法
//選完照片 或者 拍攝完成后 調(diào)用的方法
- (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 本質(zhì)是一個3*3的矩陣,通過改變其中的元素可以達到對UIView的縮放,平移,旋轉(zhuǎn)的效果,但實際上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屬性的改變可以用以上兩個矩陣相乘的結(jié)果來表示

struct CGAffineTransform {

    CGFloat a, b, c, d;// 這四個參數(shù)是用于控制transform屬性的旋轉(zhuǎn)和縮放
    CGFloat tx, ty; // 這兩個參數(shù)是用于控制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];
//相乘結(jié)果是
//x2 = x1 + tx; 在X軸移動tx個單位
//y2 = y1 + ty;  在Y軸移動ty個單位
view.transform = CGAffineTransformMakeScale(sx ,sy);// 返回矩陣[sx 0 0 sy 0 0];
//相乘結(jié)果是
//x2 = x1*sx; X軸放大(縮?。樵瓉淼膕x倍
//y2 = y1*sy; Y軸放大(縮?。樵瓉淼膕y倍
view.transform = CGAffineTransformMakeRotation(angle);// 返回矩陣[cos(angle) sin(angle) -sin(angle) cos(angle) 0 0];
//相乘結(jié)果是
//x2 = x1*cos(angle)+y1*sin(angle)
//y2 = x1*-sin(angle)+y1*cos(angle)
//旋轉(zhuǎn)angle度;
*******************************

除了上面三種外,還有三種是方法中沒有Make的,在原有參數(shù)的前面加上一個CGAffineTransform類型的參數(shù)t,返回的是在t的基礎(chǔ)上進行對應(yīng)的變化后的矩陣

這些方法在變化完成后都需要把相關(guān)對象的對應(yīng)屬性置為初值,否則下一次變化開始view會恢復原狀,如
pinch手勢的pinch.scale要置為1
rotation手勢的rotation.rotation要置為0
pan手勢的[pan setTranslation:CGPointZero inView:pan.view];將現(xiàn)在的位置置為0點;
參考資料及詳細說明
http://www.cnblogs.com/smileEvday/archive/2013/04/23/Rotate1.html
博主:一片楓葉

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容