186使用系统内的相册

PS:对于 Camera 选项,会调用摄像头,需要真机才能测试。

效果如下:

ViewController.h

1 #import <UIKit/UIKit.h>
2
3 @interface ViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
4 @property (strong, nonatomic) UIImageView *imgVCustom;
5
6 @end

ViewController.m

 1 #import "ViewController.h"
 2
 3 @interface ViewController ()
 4 - (void)layoutUI;
 5 - (void)showActionSheet:(UIBarButtonItem *)sender;
 6 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
 7 @end
 8
 9 @implementation ViewController
10
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13
14     [self layoutUI];
15 }
16
17 - (void)didReceiveMemoryWarning {
18     [super didReceiveMemoryWarning];
19     // Dispose of any resources that can be recreated.
20 }
21
22 - (void)viewWillAppear:(BOOL)animated {
23     [super viewWillAppear:animated];
24     [self.navigationController setNavigationBarHidden:NO animated:animated];
25     [self.navigationController setToolbarHidden:NO animated:animated];
26 }
27
28 - (void)layoutUI {
29     self.navigationItem.title = @"使用系统内的相册";
30     self.view.backgroundColor = [UIColor whiteColor];
31     UIBarButtonItem *barBtnChooseImage = [[UIBarButtonItem alloc]
32                                           initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
33                                           target:self
34                                           action:@selector(showActionSheet:)];
35     self.toolbarItems = @[barBtnChooseImage];
36
37     _imgVCustom = [[UIImageView alloc] initWithFrame:self.view.bounds];
38     _imgVCustom.contentMode = UIViewContentModeScaleAspectFit;
39     _imgVCustom.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
40     _imgVCustom.image = [UIImage imageNamed:@"1"];
41     [self.view addSubview:_imgVCustom];
42 }
43
44 - (void)showActionSheet:(UIBarButtonItem *)sender {
45     UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择相册"
46                                                              delegate:self
47                                                     cancelButtonTitle:@"取消"
48                                                destructiveButtonTitle:nil
49                                                     otherButtonTitles:@"PhotoLibrary", @"Camera", @"SavedPhotosAlbum", nil];
50     [actionSheet showFromToolbar:self.navigationController.toolbar];
51 }
52
53 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
54     if (error) {
55         NSLog(@"%@", [error localizedDescription]);
56     }
57 }
58
59 #pragma mark - UIActionSheetDelegate
60 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
61     if (buttonIndex != actionSheet.cancelButtonIndex) {
62         UIImagePickerControllerSourceType sourceType = buttonIndex;
63         if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
64             UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
65             imagePickerController.delegate = self;
66             imagePickerController.sourceType = sourceType;
67             imagePickerController.allowsEditing = YES;
68             [self presentViewController:imagePickerController
69                                animated:YES
70                              completion:nil];
71         }
72     }
73 }
74
75 #pragma mark - UIImagePickerControllerDelegate
76 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
77     UIImage *imgChoice = [info objectForKey:UIImagePickerControllerEditedImage];
78     if (!imgChoice) {
79         imgChoice = [info objectForKey:UIImagePickerControllerOriginalImage];
80     }
81     _imgVCustom.image = imgChoice;
82
83     //把图片保存到相册
84 //    UIImageWriteToSavedPhotosAlbum(imgChoice,
85 //                                   self,
86 //                                   @selector(image:didFinishSavingWithError:contextInfo:),
87 //                                   NULL);
88
89     [self imagePickerControllerDidCancel:picker];
90 }
91
92 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
93     [self dismissViewControllerAnimated:YES completion:nil];
94 }
95
96 @end

AppDelegate.h

1 #import <UIKit/UIKit.h>
2
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 @property (strong, nonatomic) UIWindow *window;
5 @property (strong, nonatomic) UINavigationController *navigationController;
6
7 @end

AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3
 4 @interface AppDelegate ()
 5 @end
 6
 7 @implementation AppDelegate
 8
 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
11     ViewController *viewController = [[ViewController alloc] init];
12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
13     _window.rootViewController = _navigationController;
14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
15     [_window makeKeyAndVisible];
16     return YES;
17 }
18
19 - (void)applicationWillResignActive:(UIApplication *)application {
20 }
21
22 - (void)applicationDidEnterBackground:(UIApplication *)application {
23 }
24
25 - (void)applicationWillEnterForeground:(UIApplication *)application {
26 }
27
28 - (void)applicationDidBecomeActive:(UIApplication *)application {
29 }
30
31 - (void)applicationWillTerminate:(UIApplication *)application {
32 }
33
34 @end
时间: 2024-10-10 20:44:37

186使用系统内的相册的相关文章

UIImagePickerController的用法

在实际的APP开发中,我们经常会见到应用的这样的功能 :需要选取手机相册的照片,还有选取视频,拍视频和照相的操作. 在iOS开发中,实现以上的功能就需要用到 UIImagePickerController. 现将 UIImagePickerController的基本用法总结如下 : 1 #import "ViewController.h" 2 3 @interface ViewController ()<UIImagePickerControllerDelegate,UINavi

Android 相册图片选取+自定义裁剪方式(非系统裁剪)

不多说,直接上代码(裁剪的代码摘自网络.)(项目可运行) 主要是系统自身的剪切方式在有些机型上会程序崩溃的问题. 1 package com.jichun.activity; 2 3 import java.io.FileNotFoundException; 4 5 import com.jichun.view.CropCanvas; 6 7 import android.app.Activity; 8 import android.content.ContentResolver; 9 impo

IOS研究院之打开照相机与本地相册选择图片

如下图所示 在本地相册中选择一张图片后,我们将他拷贝至沙盒当中,在客户端中将它的缩略图放在按钮旁边,这个结构其实和新浪微薄中选择图片后的效果一样.最终点击发送将按钮将图片2进制图片上传服务器. 下面我们仔细学习具体的细节.创建一个空的IOS项目,接着在创建一个ViewController. AppDelegate.h 应用的代理类 这个没什么好说的就是直接打开刚刚创建的新ViewController. 1 #import <UIKit/UIKit.h> 2 #import "Test

安卓实现二维码生成和扫描功能,扫描支持直接拍照扫码和相册图片扫码,还加了照明功能

最近在做二维码的生成和扫描,生成二维码相对而言较为简单,扫描相对复杂,遇到的问题较多,但是在实现二维码的生成和扫描之前最重要的一步 就是讲Zxing包导入,后面的内容大部分是使用包中的内容, 那我就从二维码的生成讲起吧! 二维码生成: 直接贴代码了 1 //要转换的地址或字符串,可以是中文,输入内容生成二维码 2 public Bitmap createQRImage(String string) { 3 try { 4 Hashtable<EncodeHintType, String> hi

Android手机多媒体——拍照和相册

一 拍照功能 1.布局文件:在线性布局中设置一个按钮,用来启动拍照功能,设置一个ImageView用来展示图像 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orie

使用CSS3实现一个3D相册

CSS3系列我已经写过两篇文章,感兴趣的同学可以先看一下CSS3初体验之奇技淫巧,CSS3 3D立方体效果-transform也不过如此 第一篇主要列出了一些常用或经典的CSS3技巧和方法:第二篇是一个用CSS3实现的立方体实例,详细讲解了3D旋转和transform等属性. 本文再来利用CSS3属性来编写一个实例,话不多说,先直接看看效果.3D相册实例DEMO 因为前面已经讲解过一些属性的用法,此篇文章不再赘述,只记录这个实例的编码过程.项目代码已上传至github,项目代码github地址

iOS 下的相册与图片处理

iOS 下的相册与图片处理 需求 很多公司项目中都会使用到相册,以及相机,保存图片,从相册中选取图片等等操作.本文将详细介绍该功能如何实现优化,以及使用一些优秀的第三方库来辅助完成我们的需求. photos framework 的使用 Photos Framework reference Classes PHAdjustmentData /* When a user edits an asset, Photos saves a PHAdjustmentData object along with

iOS开发之保存照片到系统相册(Photo Album)

iOS开发之保存照片到系统相册(Photo Album) 保存照片到系统相册这个功能很多社交类的APP都有的,今天我们简单讲解一下,如何将图片保存到系统相册(Photo Album). 创建UIImageView 创建UIImageView是为了将照片展示出来,我们是要把UIImage保存到系统相册(Photo Album): #define SCREEN [UIScreen mainScreen].bounds.size self.image = [UIImage imageNamed:@"i

IOS调用相机相册

#import "SendViewController.h"  //只能打开,没有加载图片的代码,老代码,供参考 #import <MobileCoreServices/UTCoreTypes.h> @interface SendViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate> -(IBAction)sel