一、新建工程
二、拖控件,创建映射
三、在.h中加入delegate
1. @interface ViewController : UIViewController
复制代码
四、实现按钮事件
1. -(IBAction)chooseImage:(id)sender {
2.
3. UIActionSheet *sheet;
4.
5. // 判断是否支持相机
6.
7. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
8.
9. {
10. sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
11.
12. }
13.
14. else {
15.
16. sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
17.
18. }
19.
20. sheet.tag = 255;
21.
22. [sheet showInView:self.view];
23.
24. }
复制代码
五、实现actionSheet delegate事件
1. -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
2. {
3. if (actionSheet.tag == 255) {
4.
5. NSUInteger sourceType = 0;
6.
7. // 判断是否支持相机
8. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
9.
10. switch (buttonIndex) {
11. case 0:
12. // 取消
13. return;
14. case 1:
15. // 相机
16. sourceType = UIImagePickerControllerSourceTypeCamera;
17. break;
18.
19. case 2:
20. // 相册
21. sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
22. break;
23. }
24. }
25. else {
26. if (buttonIndex == 0) {
27.
28. return;
29. } else {
30. sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
31. }
32. }
33. // 跳转到相机或相册页面
34. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
35.
36. imagePickerController.delegate = self;
37.
38. imagePickerController.allowsEditing = YES;
39.
40. imagePickerController.sourceType = sourceType;
41.
42. [self presentViewController:imagePickerController animated:YES completion:^{}];
43.
44. [imagePickerController release];
45. }
46. }
复制代码
六、实现ImagePicker delegate 事件
1. #pragma mark - image picker delegte
2. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
3. {
4. [picker dismissViewControllerAnimated:YES completion:^{}];
5.
6. UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
7. /* 此处info 有六个值
8. * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
9. * UIImagePickerControllerOriginalImage; // a UIImage 原始图片
10. * UIImagePickerControllerEditedImage; // a UIImage 裁剪后图片
11. * UIImagePickerControllerCropRect; // an NSValue (CGRect)
12. * UIImagePickerControllerMediaURL; // an NSURL
13. * UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework
14. * UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo
15. */
16. // 保存图片至本地,方法见下文
17. [self saveImage:image withName:@"currentImage.png"];
18.
19. NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
20.
21. UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
22.
23. isFullScreen = NO;
24. [self.imageView setImage:savedImage];
25.
26. self.imageView.tag = 100;
27.
28. }
29. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
30. {
31. [self dismissViewControllerAnimated:YES completion:^{}];
32. }
复制代码
七、保存图片
高保真压缩图片方法
1. NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality
2. )
复制代码
此方法可将图片压缩,但是图片质量基本不变,第二个参数即图片质量参数。
1. #pragma mark - 保存图片至沙盒
2. - (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
3. {
4.
5. NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
6. // 获取沙盒目录
7.
8. NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
9. // 将图片写入文件
10.
11. [imageData writeToFile:fullPath atomically:NO];
12. }
复制代码
八、实现点击图片预览功能,滑动放大缩小,带动画
1. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
2. {
3.
4. isFullScreen = !isFullScreen;
5. UITouch *touch = [touches anyObject];
6.
7. CGPoint touchPoint = [touch locationInView:self.view];
8.
9. CGPoint imagePoint = self.imageView.frame.origin;
10. //touchPoint.x ,touchPoint.y 就是触点的坐标
11.
12. // 触点在imageView内,点击imageView时 放大,再次点击时缩小
13. if(imagePoint.x <= touchPoint.x && imagePoint.x +self.imageView.frame.size.width >=touchPoint.x && imagePoint.y <= touchPoint.y && imagePoint.y+self.imageView.frame.size.height >= touchPoint.y)
14. {
15. // 设置图片放大动画
16. [UIView beginAnimations:nil context:nil];
17. // 动画时间
18. [UIView setAnimationDuration:1];
19.
20. if (isFullScreen) {
21. // 放大尺寸
22.
23. self.imageView.frame = CGRectMake(0, 0, 320, 480);
24. }
25. else {
26. // 缩小尺寸
27. self.imageView.frame = CGRectMake(50, 65, 90, 115);
28. }
29.
30. // commit动画
31. [UIView commitAnimations];
32.
33. }
34.
35. }
复制代码
九、上传图片,使用ASIhttpRequest类库实现,由于本文重点不是网络请求,故不对ASIHttpRequest详细讲述,只贴出部分代码
1. ASIFormDataRequest *requestReport = [[ASIFormDataRequest alloc] initWithURL:服务器地址];
2.
3. NSString *Path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
4.
5. [requestReport setFile:Path forKey:@"picturepath"];
6.
7. [requestReport buildPostBody];
8.
9. requestReport.delegate = self;
10.
11. [requestReport startAsynchronous];
复制代码
效果图如下:
ps:
1.模拟器无法调用相机;
2.模拟器添加图片方法:将图片拖至模拟器主屏,会由模拟器safari打开,长按可保存至模拟器相册,即可进行模拟器调试了。
=============================================================
今天调用系统的照相和本地相册,编辑图片的功能,但是按钮都是英文,我想改为中文的 取消,选择,重拍
需要改变两种方法
1. UIImagePickerController页面的Cancel和Choose按钮以及截取中得重拍按钮,想改成中文
本来打算通过获取这些按钮的指针进行设置的,最后发现可以在工程中直接 project-->info-->Localization--->language中add一个简体中文就可以了
参考:http://www.cocoachina.com/bbs/read.php?tid=132828
2.定制当然也有方法
首先通过递归的方法不断地在self.view的hierarchy中不断遍历,直到查找到这个view,获取其指针进行操作
|