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 ...
随机推荐
- [转] Web前端优化之 Javascript篇
原文链接: http://lunax.info/archives/3099.html Web 前端优化最佳实践之 JavaScript 篇,这部分有 6 条规则,和 CSS 篇 重复的有几条.前端优化 ...
- MYSQL数据库性能调优之四:解决慢查询--索引
为什么索引能够提高查询速度?没有索引 检索数据的方式是从头到尾一条一条挨着匹配,这是慢的根本原因:索引类型BTREE:二叉树类型,原理图如下:对表创建一个二叉树,记录中间数据的物理磁盘地址,二叉树检索 ...
- codeforces 601A The Two Routes(最短路 flody)
A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Live m3u8播放3个文件自动停止问题
Live m3u8播放3个文件自动停止问题 1.问题描述 最近做一个转码切片播放测试,使用HLS(HTTP Live Streaming)来做直播, 每个TS分片时间为10s,根据TS分片文件生成以下 ...
- enumerate
enumerate 函数用于遍历序列中的元素并分配一个序号(序号默认从零开始 可以制定任意值): >>> for i,j in enumerate(('a','b','c')): p ...
- MSSQL手札一 MSSQL的游标
和oracle的不同,MSSQL中没有隐式游标的说法,也没有%type和%rowtype这样根据数据库字段实时更新变量含义的关键字,MSSQL的游标类似于oracle的显示游标,需要自己去手动关闭, ...
- tableviewcell 系统工具删除:左滑删除,选中多个删除
在编辑tableview的时候,我们有时候会需要编辑对一些cell进行删除(增加),在这个时候,我们最好用系统的方法来进行增删的操作 // // text1Controller.m // text / ...
- sql server对并发的处理-乐观锁和悲观锁【粘】
假如两个线程同时修改数据库同一条记录,就会导致后一条记录覆盖前一条,从而引发一些问题. 例如: 一个售票系统有一个余票数,客户端每调用一次出票方法,余票数就减一. 情景: 总共300张票,假设两个售票 ...
- C#使用SMTP发送邮件
需要用到的命名空间: using System.Net.Mail; using System.IO; using System.Text.RegularExpressions; using Syste ...
- Visual Studio 2012 主题下的代码配色方案
默认的VS2012的深色配色方案个人感觉很丑,不是很好看,于是就自己动手配置了一下,突出语法高亮显示,增加代码语法识别度,个人感觉还是可以的. 原来使用的是VAX,但自从VAX导致的我的VS不能输入中 ...