IOS实用功能之截图(来自相册和拍照)
//
// ViewController.m
// MyImagePicker1.0
//
// Created by Mac on 14-7-14.
// Copyright (c) 2014年 Digital media technology. All rights reserved.
//
#import "ViewController.h"
#import "VPImageCropperViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
#define ORIGINAL_MAX_WIDTH 640.0f
@interfaceViewController () <UINavigationControllerDelegate,UIImagePickerControllerDelegate,
UIActionSheetDelegate,VPImageCropperDelegate>
@property (weak, nonatomic)IBOutletUIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//do some init job for UIImageView
[self.imageView.layersetCornerRadius:(self.imageView.frame.size.height/2)];
[self.imageView.layersetMasksToBounds:YES];
[self.imageViewsetContentMode:UIViewContentModeScaleAspectFill];
[self.imageViewsetClipsToBounds:YES];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizeralloc]
initWithTarget:selfaction:@selector(tapGRAction)];
self.imageView.layer.borderWidth = 1.0;
self.imageView.layer.borderColor = [[UIColorblackColor]
CGColor];
[self.imageViewaddGestureRecognizer:tapGR];
//load an image
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
NSURL *portraitUrl = [NSURLURLWithString:@"http://photo.l99.com/bigger/31/1363231021567_5zu910.jpg"];
UIImage *protraitImg = [UIImageimageWithData:[NSDatadataWithContentsOfURL:portraitUrl]];
dispatch_sync(dispatch_get_main_queue(), ^{
self.imageView.image = protraitImg;
});
});
}
-(void)tapGRAction{
NSLog(@"tap");
UIActionSheet *actionSheet = [[UIActionSheetalloc]
initWithTitle:@"选择"delegate:selfcancelButtonTitle:@"Cancel"destructiveButtonTitle:nilotherButtonTitles:@"Take
photo",@"From Albums",
nil];
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
NSLog(@"Take photo");
// 拍照
if ([selfisCameraAvailable] && [selfdoesCameraSupportTakingPhotos])
{
UIImagePickerController *controller = [[UIImagePickerControlleralloc]
init];
controller.sourceType =UIImagePickerControllerSourceTypeCamera;
if ([selfisFrontCameraAvailable]) {
controller.cameraDevice =UIImagePickerControllerCameraDeviceFront;
}
NSMutableArray *mediaTypes = [[NSMutableArrayalloc]
init];
[mediaTypes addObject:(__bridgeNSString *)kUTTypeImage];
controller.mediaTypes = mediaTypes;
controller.delegate =
self;
[self presentViewController:controller
animated:YES
completion:^(void){
NSLog(@"Picker View Controller is presented");
}];
}
break;
case 1:
NSLog(@"Albums");
// 从相册中选取
if ([selfisPhotoLibraryAvailable]) {
UIImagePickerController *controller = [[UIImagePickerControlleralloc]
init];
controller.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
NSMutableArray *mediaTypes = [[NSMutableArrayalloc]
init];
[mediaTypes addObject:(__bridgeNSString *)kUTTypeImage];
controller.mediaTypes = mediaTypes;
controller.delegate =
self;
[self presentViewController:controller
animated:YES
completion:^(void){
NSLog(@"Picker View Controller is presented");
}];
}
break;
default:
break;
}
}
#pragma mark VPImageCropperDelegate
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {
self.imageView.image = editedImage;
// [self.imageView.layer setCornerRadius:(self.imageView.frame.size.height/2)];
// [self.imageView.layer setMasksToBounds:YES];
// [self.imageView setContentMode:UIViewContentModeScaleAspectFill];
// [self.imageView setClipsToBounds:YES];
[cropperViewController dismissViewControllerAnimated:YEScompletion:^{
// TO DO
}];
}
- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
[cropperViewController dismissViewControllerAnimated:YEScompletion:^{
}];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YEScompletion:^() {
UIImage *portraitImg = [infoobjectForKey:@"UIImagePickerControllerOriginalImage"];
portraitImg = [self
imageByScalingToMaxSize:portraitImg];
// 裁剪
VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewControlleralloc]
initWithImage:portraitImg cropFrame:CGRectMake(0, 100.0f,
self.view.frame.size.width,self.view.frame.size.width)limitScaleRatio:3.0];
imgEditorVC.delegate =
self;
[selfpresentViewController:imgEditorVC
animated:YEScompletion:^{
// TO DO
}];
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YEScompletion:^(){
}];
}
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
}
#pragma mark camera utility
- (BOOL) isCameraAvailable{
return [UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
- (BOOL) isRearCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}
- (BOOL) isFrontCameraAvailable {
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}
- (BOOL) doesCameraSupportTakingPhotos {
return [selfcameraSupportsMedia:(__bridgeNSString
*)kUTTypeImagesourceType:UIImagePickerControllerSourceTypeCamera];
}
- (BOOL) isPhotoLibraryAvailable{
return [UIImagePickerControllerisSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) canUserPickVideosFromPhotoLibrary{
return [self
cameraSupportsMedia:(__bridgeNSString *)kUTTypeMoviesourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) canUserPickPhotosFromPhotoLibrary{
return [self
cameraSupportsMedia:(__bridgeNSString *)kUTTypeImagesourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) cameraSupportsMedia:(NSString *)paramMediaType sourceType:(UIImagePickerControllerSourceType)paramSourceType{
__block BOOL result =NO;
if ([paramMediaType length] == 0) {
return NO;
}
NSArray *availableMediaTypes = [UIImagePickerControlleravailableMediaTypesForSourceType:paramSourceType];
[availableMediaTypes enumerateObjectsUsingBlock: ^(id obj,NSUInteger idx,
BOOL *stop) {
NSString *mediaType = (NSString *)obj;
if ([mediaType isEqualToString:paramMediaType]){
result = YES;
*stop= YES;
}
}];
return result;
}
#pragma mark image scale utility
- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage {
if (sourceImage.size.width <ORIGINAL_MAX_WIDTH)
return sourceImage;
CGFloat btWidth = 0.0f;
CGFloat btHeight = 0.0f;
if (sourceImage.size.width > sourceImage.size.height) {
btHeight = ORIGINAL_MAX_WIDTH;
btWidth = sourceImage.size.width * (ORIGINAL_MAX_WIDTH / sourceImage.size.height);
} else {
btWidth = ORIGINAL_MAX_WIDTH;
btHeight = sourceImage.size.height * (ORIGINAL_MAX_WIDTH / sourceImage.size.width);
}
CGSize targetSize = CGSizeMake(btWidth, btHeight);
return [selfimageByScalingAndCroppingForSourceImage:sourceImage
targetSize:targetSize];
}
- (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize {
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint =
CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) ==NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize);// this will crop
CGRect thumbnailRect =
CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage ==nil)
NSLog(@"could not scale image");
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark portraitImageView getter
- (UIImageView *)portraitImageView {
if (!self.imageView) {
CGFloat w = 100.0f;
CGFloat h = w;
CGFloat x = (self.view.frame.size.width - w) / 2;
CGFloat y = (self.view.frame.size.height - h) / 2;
self.imageView = [[UIImageViewalloc]
initWithFrame:CGRectMake(x, y, w, h)];
//把图像变成圆形的代码
[self.imageView.layersetCornerRadius:(self.imageView.frame.size.height/2)];
[self.imageView.layersetMasksToBounds:YES];
[self.imageViewsetContentMode:UIViewContentModeScaleAspectFill];
[self.imageViewsetClipsToBounds:YES];
//设置阴影
self.imageView.layer.shadowColor = [UIColorblackColor].CGColor;
self.imageView.layer.shadowOffset =CGSizeMake(4, 4);
self.imageView.layer.shadowOpacity = 0.5;
self.imageView.layer.shadowRadius = 2.0;
//设置边框的大小和颜色
self.imageView.layer.borderColor = [[UIColorblackColor]
CGColor];
self.imageView.layer.borderWidth = 2.0f;
self.imageView.userInteractionEnabled =YES;
self.imageView.backgroundColor = [UIColorblackColor];
UITapGestureRecognizer *portraitTap = [[UITapGestureRecognizeralloc]
initWithTarget:selfaction:@selector(editPortrait)];
[self.imageViewaddGestureRecognizer:portraitTap];
}
returnself.imageView;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
IOS实用功能之截图(来自相册和拍照)的更多相关文章
- IOS编程之相机和相册
概述 IOS设备中的相机和相册,是我们在项目开发中经常会使用到的多媒体元素,使用相机可以获得最新想要的照片,而使用相册则可以访问IOS设备中的图片资源 使用IOS设备中的相机/相册获得图片资源 是否允 ...
- 移动端js调取手机相册和拍照功能
前端可以通过js调取手机的相册和拍照功能,但不能拍视频!!! <!DOCTYPE html> <html lang="en"> <head> & ...
- 利用 AFN 上传相册或拍照图片
概述 自定义上传图片请求,自定义调取相册及拍照,方便多处使用时调用. 详细 代码下载:http://www.demodashi.com/demo/10718.html 由于项目中多处需要上传图片,我们 ...
- 转载:Android调用相册、拍照实现缩放、切割图片
好几天没有写博客了,感觉都有点懈怠了.笔者参加了大学生第二届软件设计大赛,这几天 一直在弄大赛的事情,没有花些时间来整理博客.好在经过一些时日比赛的东西也弄得差不多了, 接下来就是将这段时间学习里面有 ...
- Android调用系统相册和拍照的Demo
最近我在群里看到有好几个人在交流说现在网上的一些Android调用系统相册和拍照的demo都有bug,有问题,没有一个完整的.确实是,我记得一个月前,我一同学也遇到了这样的问题,在低版本的系统中没问题 ...
- iOS开发—— UIImagePickerController获取相册和拍照
一.简单的拍照显示,或是从相册中直接选取照片 #import "ViewController.h" @interface ViewController ()<UIImageP ...
- ios开发将截图保存到相册
- (void)loadImageFinished:(UIImage *)image { UIImageWriteToSavedPhotosAlbum(image, self, @selector(i ...
- ios uiimagepickercontroller 选择相册或者拍照上传
首先需要实现UIImagePickerControllerDelegate 代理 实现其imagePickerController 方法 这里用于选择图片或的拍照回调 //调用相机拍照 或者 图库选 ...
- IOS 获取系统相册和拍照使用HXPhotoPicker 返回页面时页面上移被nav遮住问题
解决: - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.automaticallyAdj ...
随机推荐
- Fedora20 编译安装qemu-system
安装简介: 1.1. 本次编译安装所有的操作都在Fedora 20 x86-64上,内核版本为: 3.14.4-200.fc20.x86_64.如果在其他系统编译安装,请看其他文章. 2.安装准备: ...
- win7 下启动mysql
1.下载mysql最新版,解压,不用安装. 2.启动服务: 进入bin文件夹下,使用管理员权限运行 mysqld.exe. 3.测试是否启动了服务. 4.登录mysql.初次安装的,没有设置密码,直接 ...
- 《Linux设备驱动程序》 笔记2
驱动代码hello.c #include <linux/init.h> #include <linux/module.h> static int hello_init(void ...
- NHibernate的简单例子
NHibernate的简单例子 @(编程) [TOC] 因为项目需求,搭了一个NHibernate的例子,中间遇到了一些问题,通过各种方法解决了,在这里记录一下最后的结果. 1. 需要的dll Com ...
- hdu 1199 Color the Ball
http://acm.hdu.edu.cn/showproblem.php?pid=1199 Color the Ball Time Limit: 2000/1000 MS (Java/Others) ...
- CIDR
CIDR的介绍: CIDR(Classless Inter-Domain Routing,无类域间路由选择)它消除了传统的A类.B类和C类地址以及划分子网的概念,因而可以更加有效地分配IPv4的地址空 ...
- 实时监控MySql状态
大多网站的性能瓶颈都会出在数据库上,所以想把Mysql监控起来,就搜索了下相关资料. 后来和同事讨论了下cacti和nagios有些老套和过时,graphite比较时尚,然后就搜了下相关的资料,最后搞 ...
- HDU1712简单的分组背包
HDU1712http://acm.hdu.edu.cn/showproblem.php?pid=1712 简单的分组背包 #include <map> #include <set& ...
- 转载linq to sql 的详解
[转]LINQ To SQL 语法及实例大全 2011-11-26阅读38651 评论9 LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL ...
- [Mac]Mac Xcode 删除已经下载好的模拟器版本
Delete simulator refences for xCode: Delete the particular simulator runtime references (*.simruntim ...