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 ...
随机推荐
- Java与云计算有什么关系呢
在如今这个信息技术高速发展的今天,云计算已经不是一个陌生的概念了,但是,当云计算遇到java将会有什么样的问题产生呢?下面,新霸哥将会为你揭晓Java与云计算之间的关系. 众所周知,java是一种应用 ...
- SparkR grammer
They are different between local R and sparkR: sparkR 跑通的函数: http://blog.csdn.net/wa2003/article/det ...
- Pregel: A System for Large-Scale Graph Processing(译)
[说明:Pregel这篇是发表在2010年的SIGMOD上,Pregel这个名称是为了纪念欧拉,在他提出的格尼斯堡七桥问题中,那些桥所在的河就叫Pregel.最初是为了解决PageRank计算问题,由 ...
- 55个高质量的Magento主题,助你构建电子商务站点
Magento是一个功能丰富的开源电子商务平台(译者注:基于PHP的Zend Framework开发),在网店的外观.商品管理以及其它功能上,它给商家提供了前所未有的灵活和易用性.通过挑选一个合适的M ...
- C语言基础(不断更新)
1.memcpy. memmove.memccpy的区别 字符串函数功能查询 memcpy要求源串和目的串不能重叠 memccpy:copy直至遇到由参数指定的ch. memmove: 源串和目的串可 ...
- PHPCMS数据筛选功能实现
第一步:添加模型字段,这个模型可以是官方的,也可以是你自定义的模型,以单选字段形式添加就好了; 第二步:就是添加栏目和内容: 第三步:模板如下,照着改就好了. {template "cont ...
- Windows Azure 虚拟网络配置(Site to Site)
上篇我们创建了Point to Site的虚拟网络连接,来满足客户端到云端网络的连接.本篇文章我们将创建Site to Site的虚拟网络连接,以满足本地网络到云端的网络连接. 创建与配置过程与上篇较 ...
- 前端复习-02-ajax原生以及jq和跨域方面的应用。
ajax这块我以前一直都是用现成的jq封装好的东西,而且并没有深入浅出的研究过,也没有使用过原生形式的实现.包括了解jsonp和跨域的相关概念但是依然没有实现过,其中有一个重要的原因我认为是我当时并不 ...
- 转】Maven学习总结(九)——使用Nexus搭建Maven私服
原博文出自于:http://www.cnblogs.com/xdp-gacl/p/4068967.html 感谢! 一.搭建nexus私服的目的 为什么要搭建nexus私服,原因很简单,有些公司都不提 ...
- Leveldb Advanced
[Slice] The return value of the it->key() and it->value() is a simple structure that contains ...