相冊相機
需要遵守兩個協議 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
博主:一片楓葉