参照文章:http://www.cnblogs.com/gcb999/p/3223469.html
http://blog.sina.com.cn/s/blog_9693f61a01016alr.html
@interface ViewController ()
@end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame=CGRectMake(0, 0, 200, 50); [button setTitle:@"aa" forState:UIControlStateNormal]; [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } //调动摄像头拍摄 -(void)click:(UIButton *)btn{ UIImagePickerController *imagePicker=[[UIImagePickerController alloc] init]; imagePicker.delegate=self; // imagePicker.view.frame=s if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera; } // imagePicker.allowsEditing=YES; // [self.view addSubview:imagePicker.view]; [self presentViewController:imagePicker animated:YES completion:^{ }];
#pragma mark------从相册选择------------
- (void) chooseFromAlbum {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
//从相册列表选取
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//此处设置只能使用相机,禁止使用视频功能
// picker.mediaTypes = [[NSArray alloc]initWithObjects:(NSString *)kUTTypeImage,nil];
}
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%@",info);
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
self.image.image=image;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:^{
}];
}
#pragma mark---保存相册到本地-------
- (void)saveImage
{
int index = _scrollView.contentOffset.x / _scrollView.bounds.size.width;
ZoomScroll *currentImageView = _scrollView.subviews[index];
UIImageWriteToSavedPhotosAlbum(currentImageView.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] init];
indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
indicator.center = self.center;
_indicatorView = indicator;
[[UIApplication sharedApplication].keyWindow addSubview:indicator];
[indicator startAnimating];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
{
[_indicatorView removeFromSuperview];
UILabel *label = [[UILabel alloc] init];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];
label.layer.cornerRadius = 5;
label.clipsToBounds = YES;
label.bounds = CGRectMake(0, 0, 150, 30);
// label.frame = SIZE_RectMake((ScreenWidth - label.bounds.size.width)/2, -10, 150, 30);
label.center = self.center;
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:17];
[[UIApplication sharedApplication].keyWindow addSubview:label];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:label];
if (error) {
label.text = SDPhotoBrowserSaveImageFailText;
} else {
label.text = SDPhotoBrowserSaveImageSuccessText;
}
// [UIView animateWithDuration:0.4 animations:^{
// label.frame = SIZE_RectMake((ScreenWidth - label.bounds.size.width)/2, (ScreenHeight - 30)/2, 150, 30);
// }];
// // 延迟2秒执行
//
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [UIView animateWithDuration:0.25 animations:^{
//
// label.frame = SIZE_RectMake((ScreenWidth - label.bounds.size.width)/2, (ScreenHeight - 30)/2 - 30, 150, 30);
// NSLog(@"11111");
//
// }];
// });
//
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW + 0.4, (int64_t)(2.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [UIView animateWithDuration:0.4 animations:^{
// NSLog(@"2222");
// label.frame = SIZE_RectMake((ScreenWidth - label.bounds.size.width)/2 , (ScreenHeight + 5), 150, 30);
//
// }];
// });
//
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW + 5, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
// NSLog(@"33333");
// [label removeFromSuperview];
//
// });
[label performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2.0];
}