iosios 上传图片到阿里云云,返回的图片链接

当前位置: &
& 阿里云OSS 通过表单直接上传文件 Post Policy
$access_id = 'ACCESS_ID';
$access_key = 'OSS_ACCESS_KEY';
$url='http://ioutsider.oss-cn-';//更改成你自己的地址
$policy = '{&expiration&: &T12:00:00.000Z&,&conditions&:[{&bucket&: &ioutsider& },[&content-length-range&, 0, ]]}';
$policy = base64_encode($policy);
$signature = base64_encode(hash_hmac('sha1', $policy, $access_key, true));//生成认证签名
&!DOCTYPE html&
&title&&/title&
&meta http-equiv=&Content-Type& content=&text/ charset=UTF-8&&
&div&文件上传&/div&
&form action=&&?php echo $url;?&& method=&post& enctype=&multipart/form-data&&
&label for=&file&&选择文件:&/label&
&input type=&hidden& name=&OSSAccessKeyId& id=&OSSAccessKeyId&
value=&&?php echo $access_id; ?&& /&
&input type=&hidden& name=&policy& id=&policy&
value='&?php echo $policy; ?&' /&
&input type=&hidden& name=&signature& id=&signature&
value=&&?php echo $signature; ?&& /&
&input type=&hidden& name=&key& id=&key&
value=&${filename}& /&
&input type=&file& name=&file& id=&file& /&
&input type=&submit& name=&submit& value=&确定& /&
您也许还喜欢:
微信公众平台
Powered by
Designed byiPhone开发中遇到上传图片问题,找到多资料,最终封装了一个类,请大家指点,代码如下
RequestPostUploadHelper.h
Created by 张浩 on 13-5-8.
Copyright (c) 2013年 张浩. All rights reserved.
#import &Foundation/Foundation.h&
@interface RequestPostUploadHelper : NSObject
*POST 提交 并可以上传图片目前只支持单张
+ (NSString *)postRequestWithURL: (NSString *)url
postParems: (NSMutableDictionary *)postParems // IN 提交参数据集合
picFilePath: (NSString *)picFilePath
// IN 上传图片路径
picFileName: (NSString *)picFileN
// IN 上传图片名称
* 修发图片大小
+ (UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newS
* 保存图片
+ (NSString *)saveImage:(UIImage *)tempImage WithName:(NSString *)imageN
* 生成GUID
+ (NSString *)generateUuidS
RequestPostUploadHelper.m
Created by 张浩 on 13-5-8.
Copyright (c) 2013年 张浩. All rights reserved.
#import &RequestPostUploadHelper.h&
@implementation RequestPostUploadHelper
static NSString * const FORM_FLE_INPUT = @&file&;
+ (NSString *)postRequestWithURL: (NSString *)url
postParems: (NSMutableDictionary *)postParems // IN
picFilePath: (NSString *)picFilePath
picFileName: (NSString *)picFileN
NSString *TWITTERFON_FORM_BOUNDARY = @&<span style="color: #xKhTmLbOuNdArY&;
//根据url初始化request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:<span style="color: #];
//分界线 --AaB03x
NSString *MPboundary=[[NSString alloc]initWithFormat:@&--%@&,TWITTERFON_FORM_BOUNDARY];
//结束符 AaB03x--
NSString *endMPboundary=[[NSString alloc]initWithFormat:@&%@--&,MPboundary];
//得到图片的data
if(picFilePath){
UIImage *image=[UIImage imageWithContentsOfFile:picFilePath];
//判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image)) {
//返回为png图像。
data = UIImagePNGRepresentation(image);
//返回为JPEG图像。
data = UIImageJPEGRepresentation(image, <span style="color: #.0);
//http body的字符串
NSMutableString *body=[[NSMutableString alloc]init];
//参数的集合的所有key的集合
NSArray *keys= [postParems allKeys];
//遍历keys
for(int i=<span style="color: #;i&[keys count];i++)
//得到当前key
NSString *key=[keys objectAtIndex:i];
//添加分界线,换行
[body appendFormat:@&%@\r\n&,MPboundary];
//添加字段名称,换2行
[body appendFormat:@&Content-Disposition: form- name=\&%@\&\r\n\r\n&,key];
//添加字段的值
[body appendFormat:@&%@\r\n&,[postParems objectForKey:key]];
NSLog(@&添加字段的值==%@&,[postParems objectForKey:key]);
if(picFilePath){
////添加分界线,换行
[body appendFormat:@&%@\r\n&,MPboundary];
//声明pic字段,文件名为boris.png
[body appendFormat:@&Content-Disposition: form- name=\&%@\&; filename=\&%@\&\r\n&,FORM_FLE_INPUT,picFileName];
//声明上传文件的格式
[body appendFormat:@&Content-Type: image/jpge,image/gif, image/jpeg, image/pjpeg, image/pjpeg\r\n\r\n&];
//声明结束符:--AaB03x--
NSString *end=[[NSString alloc]initWithFormat:@&\r\n%@&,endMPboundary];
//声明myRequestData,用来放入http body
NSMutableData *myRequestData=[NSMutableData data];
//将body字符串转化为UTF8格式的二进制
[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
if(picFilePath){
//将image的data加入
[myRequestData appendData:data];
//加入结束符--AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//设置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@&multipart/form- boundary=%@&,TWITTERFON_FORM_BOUNDARY];
//设置HTTPHeader
[request setValue:content forHTTPHeaderField:@&Content-Type&];
//设置Content-Length
[request setValue:[NSString stringWithFormat:@&%d&, [myRequestData length]] forHTTPHeaderField:@&Content-Length&];
//设置http body
[request setHTTPBody:myRequestData];
//http method
[request setHTTPMethod:@&POST&];
NSHTTPURLResponse *urlResponese =
NSError *error = [[NSError alloc]init];
NSData* resultData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponese error:&error];
NSString* result= [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
if([urlResponese statusCode] &=<span style="color: #0&&[urlResponese statusCode]&<span style="color: #0){
NSLog(@&返回结果=====%@&,result);
* 修发图片大小
+ (UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newSize{
newSize.height=image.size.height*(newSize.width/image.size.width);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(<span style="color: #, <span style="color: #, newSize.width, newSize.height)];
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
* 保存图片
+ (NSString *)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName{
NSData* imageD
//判断图片是不是png格式的文件
if (UIImagePNGRepresentation(tempImage)) {
//返回为png图像。
imageData = UIImagePNGRepresentation(tempImage);
//返回为JPEG图像。
imageData = UIImageJPEGRepresentation(tempImage, <span style="color: #.0);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString* documentsDirectory = [paths objectAtIndex:<span style="color: #];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
NSArray *nameAry=[fullPathToFile componentsSeparatedByString:@&/&];
NSLog(@&===fullPathToFile===%@&,fullPathToFile);
NSLog(@&===FileName===%@&,[nameAry objectAtIndex:[nameAry count]-<span style="color: #]);
[imageData writeToFile:fullPathToFile atomically:NO];
return fullPathToF
* 生成GUID
+ (NSString *)generateUuidString{
// create a new UUID which you own
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
// create a new CFStringRef (toll-free bridged to NSString)
// that you own
NSString *uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
// transfer ownership of the string
// to the autorelease pool
[uuidString autorelease];
// release the UUID
CFRelease(uuid);
return uuidS
UploadViewController.h
Created by 张浩 on 13-5-6.
Copyright (c) 2013年 张浩. All rights reserved.
#import &UIKit/UIKit.h&
@interface UploadViewController : UIViewController&UIActionSheetDelegate,UIImagePickerControllerDelegate&
- (IBAction)onClickUploadPic:(id)
- (void) snapI//拍照
- (void) pickI//从相册里找
- (UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newS
- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageN
- (IBAction)onPostData:(id)
- (NSString *)generateUuidS
UploadViewController.m
Created by 张浩 on 13-5-6.
Copyright (c) 2013年 张浩. All rights reserved.
#import &UploadViewController.h&
#import &RequestPostUploadHelper.h&
@interface UploadViewController ()
NSString *TMP_UPLOAD_IMG_PATH=@&&;
@implementation UploadViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (IBAction)onClickUploadPic:(id)sender {
UIActionSheet *menu=[[UIActionSheet alloc] initWithTitle:@&上传图片& delegate:self cancelButtonTitle:@&取消& destructiveButtonTitle:nil otherButtonTitles:@&拍照上传&,@&从相册上传&, nil];
menu.actionSheetStyle=UIActionSheetStyleBlackT
[menu showInView:self.view];
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@&<span style="color: #&);
if(buttonIndex==<span style="color: #){
[self snapImage];
NSLog(@&<span style="color: #&);
}else if(buttonIndex==<span style="color: #){
[self pickImage];
NSLog(@&<span style="color: #&);
[actionSheet release];
- (void) snapImage{
UIImagePickerController *ipc=[[UIImagePickerController alloc] init];
ipc.sourceType=UIImagePickerControllerSourceTypeC
ipc.delegate=
ipc.allowsEditing=NO;
[self presentModalViewController:ipc animated:YES];
//从相册里找
- (void) pickImage{
UIImagePickerController *ipc=[[UIImagePickerController alloc] init];
ipc.sourceType=UIImagePickerControllerSourceTypePhotoL
ipc.delegate=
ipc.allowsEditing=NO;
[self presentModalViewController:ipc animated:YES];
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *) info{
UIImage *img=[info objectForKey:@&UIImagePickerControllerOriginalImage&];
if(picker.sourceType==UIImagePickerControllerSourceTypeCamera){
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
UIImage *newImg=[self imageWithImageSimple:img scaledToSize:CGSizeMake(<span style="color: #0, <span style="color: #0)];
[self saveImage:newImg WithName:[NSString stringWithFormat:@&%@%@&,[self generateUuidString],@&.jpg&]];
[self dismissModalViewControllerAnimated:YES];
[picker release];
-(UIImage *) imageWithImageSimple:(UIImage*) image scaledToSize:(CGSize) newSize{
newSize.height=image.size.height*(newSize.width/image.size.width);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(<span style="color: #, <span style="color: #, newSize.width, newSize.height)];
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
NSLog(@&===TMP_UPLOAD_IMG_PATH===%@&,TMP_UPLOAD_IMG_PATH);
NSData* imageData = UIImagePNGRepresentation(tempImage);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString* documentsDirectory = [paths objectAtIndex:<span style="color: #];
// Now we get the full path to the file
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// and then we write it out
TMP_UPLOAD_IMG_PATH=fullPathToF
NSArray *nameAry=[TMP_UPLOAD_IMG_PATH componentsSeparatedByString:@&/&];
NSLog(@&===new fullPathToFile===%@&,fullPathToFile);
NSLog(@&===new FileName===%@&,[nameAry objectAtIndex:[nameAry count]-<span style="color: #]);
[imageData writeToFile:fullPathToFile atomically:NO];
- (IBAction)onPostData:(id)sender {
NSMutableDictionary * dir=[NSMutableDictionary dictionaryWithCapacity:<span style="color: #];
//[dir setValue:@&save& forKey:@&m&];
[dir setValue:@&IOS上传试试& forKey:@&title&];
[dir setValue:@&IOS上传试试& forKey:@&content&];
[dir setValue:@&<span style="color: #& forKey:@&clubUserId&];
[dir setValue:@&<span style="color: #& forKey:@&clubSectionId&];
[dir setValue:@&<span style="color: #2.168.0.26& forKey:@&ip&];
[dir setValue:@&asfdfasdfasdfasdfasdfasd=& forKey:@&sid&];
NSString *url=@&http://192.168.0.26:8090/api/club/topicadd.do?m=save&;
NSLog(@&=======上传&);
if([TMP_UPLOAD_IMG_PATH isEqualToString:@&&]){
[RequestPostUploadHelper postRequestWithURL:url postParems:dir picFilePath:nil picFileName:nil];
NSLog(@&有图标上传&);
NSArray *nameAry=[TMP_UPLOAD_IMG_PATH componentsSeparatedByString:@&/&];
[RequestPostUploadHelper postRequestWithURL:url postParems:dir picFilePath:TMP_UPLOAD_IMG_PATH picFileName:[nameAry objectAtIndex:[nameAry count]-<span style="color: #]];;
- (NSString *)generateUuidString
// create a new UUID which you own
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
// create a new CFStringRef (toll-free bridged to NSString)
// that you own
NSString *uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
// transfer ownership of the string
// to the autorelease pool
[uuidString autorelease];
// release the UUID
CFRelease(uuid);
return uuidS
阅读(...) 评论()

我要回帖

更多关于 ios 上传图片到阿里云 的文章

 

随机推荐