您的位置:首页 > 移动开发 > IOS开发

iOS学习:调用相机,选择图片上传,带预览功能 ,修改界面为中文

2015-01-26 13:03 766 查看
ios使用UIImagePickerController打开图片库和相机选择图片界面为英文描述,修改为简体中文的方法:

在info.plist中添加Localizations设置item为Chinese(simplified)。

这样打开图片库或拍照的时候就可以显示简体中文了.

目录[-]

判断是否支持相机,跳转到相机或相册界面

七、保存图片高保真压缩图片方法

八、实现点击图片预览功能,滑动放大缩小,带动画

ps:模拟器添加图片

源码下载地址:

一、新建工程





二、拖控件,创建映射





三、在.h中加入delegate

1
@interface
ViewController:UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
四、实现按钮事件

01
-(IBAction)chooseImage:(id)sender
{
02
03
UIActionSheet
*sheet;
04
<p>
05
//
判断是否支持相机
06
</p>
07
if
([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
08
09
{
10
sheet
=[[UIActionSheetalloc]initWithTitle:@
"选择"
delegate:self
cancelButtonTitle:nildestructiveButtonTitle:@
"取消"
otherButtonTitles:@
"拍照"
,@
"从相册选择"
,
nil];
11
12
}
13
14
else
{
15
16
sheet
=[[UIActionSheetalloc]initWithTitle:@
"选择"
delegate:self
cancelButtonTitle:nildestructiveButtonTitle:@
"取消"
otherButtonTitles:@
"从相册选择"
,
nil];
17
18
}
19
20
sheet.tag
=255;
21
22
[sheet
showInView:self.view];
23
24
}
五、实现actionSheetdelegate事件

判断是否支持相机,跳转到相机或相册界面

01
-(
void
)
actionSheet:(UIActionSheet*)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex
02
{
03
if
(actionSheet.tag
==255){
04
05
NSUInteger
sourceType=0;
06
07
//
判断是否支持相机
08
if
([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
09
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
<p>
34
//
跳转到相机或相册页面
35
</p>
36
UIImagePickerController
*imagePickerController=[[UIImagePickerControlleralloc]init];
37
38
imagePickerController.delegate
=self;
39
40
imagePickerController.allowsEditing
=YES;
41
42
imagePickerController.sourceType
=sourceType;
43
44
[self
presentViewController:imagePickerControlleranimated:YEScompletion:^{}];
45
46
[imagePickerController
release];
47
}
48
}
六、实现ImagePickerdelegate事件

01
#pragma
mark-imagepickerdelegte
02
-
(
void
)imagePickerController:(UIImagePickerController
*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info
03
{
04
[picker
dismissViewControllerAnimated:YEScompletion:^{}];
05
06
UIImage
*image=[infoobjectForKey:UIImagePickerControllerOriginalImage];
07
/*
此处info有六个值
08
*
UIImagePickerControllerMediaType;//anNSStringUTTypeImage)
09
*
UIImagePickerControllerOriginalImage;//aUIImage原始图片
10
*
UIImagePickerControllerEditedImage;//aUIImage裁剪后图片
11
*
UIImagePickerControllerCropRect;//anNSValue(CGRect)
12
*
UIImagePickerControllerMediaURL;//anNSURL
13
*
UIImagePickerControllerReferenceURL//anNSURLthatreferencesanassetintheAssetsLibraryframework
14
*
UIImagePickerControllerMediaMetadata//anNSDictionarycontainingmetadatafromacapturedphoto
15
*/
16
//
保存图片至本地,方法见下文
17
[self
saveImage:imagewithName:@
"currentImage.png"
];
18
19
NSString
*fullPath=[[NSHomeDirectory()stringByAppendingPathComponent:@
"Documents"
]
stringByAppendingPathComponent:@
"currentImage.png"
];
20
21
UIImage
*savedImage=[[UIImagealloc]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:YEScompletion:^{}];
32
}

七、保存图片

高保真压缩图片方法

NSData*UIImageJPEGRepresentation(UIImage*image,CGFloatcompressionQuality
)

此方法可将图片压缩,但是图片质量基本不变,第二个参数即图片质量参数。

01
#pragma
mark-保存图片至沙盒
02
-
(
void
)
saveImage:(UIImage*)currentImagewithName:(NSString*)imageName
03
{
04
05
NSData
*imageData=UIImageJPEGRepresentation(currentImage,0.5);
06
//
获取沙盒目录
07
08
NSString
*fullPath=[[NSHomeDirectory()stringByAppendingPathComponent:@
"Documents"
]
stringByAppendingPathComponent:imageName];
09
//
将图片写入文件
10
11
[imageData
writeToFile:fullPathatomically:NO];
12
}

八、实现点击图片预览功能,滑动放大缩小,带动画

01
-(
void
)touchesBegan:(NSSet
*)toucheswithEvent:(UIEvent*)event
02
{
03
04
isFullScreen
=!isFullScreen;
05
UITouch
*touch=[touchesanyObject];
06
07
CGPoint
touchPoint=[touchlocationInView:self.view];
08
09
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:nilcontext: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详细讲述,只贴出部分代码

01
ASIFormDataRequest
*requestReport=[[ASIFormDataRequestalloc]initWithURL:服务器地址];
02
03
NSString
*Path=[[NSHomeDirectory()stringByAppendingPathComponent:@
"Documents"
]
stringByAppendingPathComponent:@
"currentImage.png"
];
04
05
[requestReport
setFile:PathforKey:@
"picturepath"
];
06
07
[requestReport
buildPostBody];
08
09
requestReport.delegate
=self;
10
11
[requestReport
startAsynchronous];
效果图如下:




->

















ps:模拟器添加图片

1.模拟器无法调用相机;

2.模拟器添加图片方法:将图片拖至模拟器主屏,会由模拟器safari打开,长按可保存至模拟器相册,即可进行模拟器调试了。

3.关于调用相机,是系统自带的,不知道如何修改英文标题为中文,如cancel换为取消等,望知道的大虾们不吝告知,万分感谢

源码下载地址:

115网盘礼包接收地址:http://115.com/lb/5lbqrhfl

115网盘礼包码:5lbqrhfl

code4App下载地址:http://code4app.com/ios/51a8023c6803fa2b06000000

原始地址:http://my.oschina.net/joanfen/blog/134677
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: