//

//  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实用功能之截图(来自相册和拍照)的更多相关文章

  1. IOS编程之相机和相册

    概述 IOS设备中的相机和相册,是我们在项目开发中经常会使用到的多媒体元素,使用相机可以获得最新想要的照片,而使用相册则可以访问IOS设备中的图片资源 使用IOS设备中的相机/相册获得图片资源 是否允 ...

  2. 移动端js调取手机相册和拍照功能

    前端可以通过js调取手机的相册和拍照功能,但不能拍视频!!! <!DOCTYPE html> <html lang="en"> <head> & ...

  3. 利用 AFN 上传相册或拍照图片

    概述 自定义上传图片请求,自定义调取相册及拍照,方便多处使用时调用. 详细 代码下载:http://www.demodashi.com/demo/10718.html 由于项目中多处需要上传图片,我们 ...

  4. 转载:Android调用相册、拍照实现缩放、切割图片

    好几天没有写博客了,感觉都有点懈怠了.笔者参加了大学生第二届软件设计大赛,这几天 一直在弄大赛的事情,没有花些时间来整理博客.好在经过一些时日比赛的东西也弄得差不多了, 接下来就是将这段时间学习里面有 ...

  5. Android调用系统相册和拍照的Demo

    最近我在群里看到有好几个人在交流说现在网上的一些Android调用系统相册和拍照的demo都有bug,有问题,没有一个完整的.确实是,我记得一个月前,我一同学也遇到了这样的问题,在低版本的系统中没问题 ...

  6. iOS开发—— UIImagePickerController获取相册和拍照

    一.简单的拍照显示,或是从相册中直接选取照片 #import "ViewController.h" @interface ViewController ()<UIImageP ...

  7. ios开发将截图保存到相册

    - (void)loadImageFinished:(UIImage *)image { UIImageWriteToSavedPhotosAlbum(image, self, @selector(i ...

  8. ios uiimagepickercontroller 选择相册或者拍照上传

    首先需要实现UIImagePickerControllerDelegate 代理 实现其imagePickerController 方法  这里用于选择图片或的拍照回调 //调用相机拍照 或者 图库选 ...

  9. IOS 获取系统相册和拍照使用HXPhotoPicker 返回页面时页面上移被nav遮住问题

    解决: - (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated]; self.automaticallyAdj ...

随机推荐

  1. failback 和failover

    dubbo 和motan都有在注册中心中都有这个概念 Failover 失效转移通俗地说,即当A无法为客户服务时,系统能够自动地切换,使B能够及时地顶上继续为客户提供服务,且客户感觉不到这个为他提供服 ...

  2. Chapter13:拷贝控制

    拷贝控制操作:拷贝构造函数.拷贝赋值运算符.移动构造函数.移动赋值运算符.析构函数. 实现拷贝控制操作的最困难的地方是首先认识到什么时候需要定义这些操作. 拷贝构造函数: 如果一个构造函数的第一个参数 ...

  3. 程序语言的奥妙:算法解读 ——读书笔记

    算法(Algorithm) 是利用计算机解决问题的处理步骤. 算法是古老的智慧.如<孙子兵法>,是打胜仗的算法. 算法是古老智慧的结晶,是程序的范本. 学习算法才能编写出高质量的程序. 懂 ...

  4. WebGIS的大众化服务

    WebGIS是Web技术与GIS相结合的产物.WebGIS的真正意义在于,它将GIS从专业应用推向了大众化服务,同时为地理信息共享提供了方便而有效的途径. 一.ESRI大会关于WebGIS的报告 纵观 ...

  5. 使用 XMPP 构建一个基于 web 的通知工具——转

    Inserting of file(使用 XMPP 构建一个基于 web 的通知工具.docx) failed. Please try again. http://www.ibm.com/develo ...

  6. datagrid url json

    <div class="easyui-accordion" style="width:500px;height:300px;"> <div t ...

  7. Hadoop概念学习系列之hadoop生态系统闲谈(二十五)

    分层次讲解 最底层平台 ------->hdfs  yarn  mapreduce spark 应用层-------->hbase  hive   pig   sparkSQL    nu ...

  8. 【转】MySQL索引和查询优化

    原文链接:http://www.cnblogs.com/mailingfeng/archive/2012/09/26/2704344.html 对于任何DBMS,索引都是进行优化的最主要的因素.对于少 ...

  9. C#判断当前操作系统

    private OperatingSystem os = System.Environment.OSVersion; os.Version.Major.ToString(); ) { //Win7系统 ...

  10. maven 配置文件详解

    <projectxmlns="http://maven.apache.org/POM/4.0.0 " xmlns:xsi="http://www.w3.org/20 ...