购物网站开发的背景与意义手机网站和网站一体
news/
2025/9/24 12:23:07/
文章来源:
购物网站开发的背景与意义,手机网站和网站一体,建设工程网站tc,做网站需要多少兆专线最近搞图片上传#xff0c;折腾了一个星期终于做出来了#xff0c;网上搜出来的方法几乎都是好几年前的#xff0c;试了好多都不能用#xff0c;此次把代码公布出来供大家参考。注#xff1a;部分代码是后台写的#xff0c;此方法没用到第三方库。1.图片保存到本地同时上…最近搞图片上传折腾了一个星期终于做出来了网上搜出来的方法几乎都是好几年前的试了好多都不能用此次把代码公布出来供大家参考。注部分代码是后台写的此方法没用到第三方库。1.图片保存到本地同时上传服务器(void)saveImage:(UIImage *)image {//NSLog(“保存头像”);//[userPhotoButton setImage:image forState:UIControlStateNormal];BOOL success;NSFileManager *fileManager [NSFileManager defaultManager];NSError *error;NSArray *paths NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory [paths objectAtIndex:0];NSString *imageFilePath [documentsDirectory stringByAppendingPathComponent:“selfPhoto.jpg”];NSLog(“imageFile-%”,imageFilePath);success [fileManager fileExistsAtPath:imageFilePath];if(success) {success [fileManager removeItemAtPath:imageFilePath error:error];}//UIImagesmallImage[self scaleFromImage:image toSize:CGSizeMake(80.0f, 80.0f)];//将图片尺寸改为8080UIImage *smallImage [self thumbnailWithImageWithoutScale:image size:CGSizeMake(93, 93)];[UIImageJPEGRepresentation(smallImage, 1.0f) writeToFile:imageFilePath atomically:YES];//写入文件UIImage *selfPhoto [UIImage imageWithContentsOfFile:imageFilePath];//读取图片文件self.img.image selfPhoto;NSLog(“selfPhoto %”,selfPhoto);NSURL * url [NSURL URLWithString:http://blog.sina.com.cn/tmyij];NSData *imageData UIImageJPEGRepresentation(selfPhoto,1.0f);[self uploadFileWithURL:url data:imageData];}2.下面俩个方法都是设置请求参数static NSString *boundaryStr –;static NSStringrandomIDStr ****;static NSString *uploadID “file”;(NSString *)topStringWithMimeType:(NSString *)mimeType uploadFile:(NSString *)uploadFile{NSMutableString *strM [NSMutableString string];[strM appendFormat:%%\r\n, boundaryStr, randomIDStr];[strM appendFormat:“Content-Disposition: form-data; name”%; filename%\r\n, uploadID, uploadFile];[strM appendFormat:“Content-Type: %\r\n”, mimeType];[strM appendFormat:\r\n];NSLog(%, strM);return [strM copy];}(NSString *)bottomString{NSMutableString *strM [NSMutableString string];[strM appendFormat:%%\r\n, boundaryStr, randomIDStr];[strM appendString:“Content-Disposition:form-data; name“number”\r\n”];[strM appendFormat:\r\n];[strM appendString:“1234566788”];//上传的参数parameter[strM appendFormat:\r\n];NSLog(%, strM);return [strM copy];}3.图片上传服务器(void)uploadFileWithURL:(NSURL *)url data:(NSData *)data{NSString *topStr [self topStringWithMimeType:“application/img” uploadFile:“myHead.jpg”];NSString *bottomStr [self bottomString];NSMutableData *dataM [NSMutableData data];[dataM appendData:[bottomStr dataUsingEncoding:NSUTF8StringEncoding]];[dataM appendData:[topStr dataUsingEncoding:NSUTF8StringEncoding]];[dataM appendData:data];[dataM appendData:[\r\n dataUsingEncoding:NSUTF8StringEncoding]];NSString * s [NSString stringWithFormat:%%%\r\n,boundaryStr, randomIDStr, boundaryStr];[dataM appendData:[str22 dataUsingEncoding:NSUTF8StringEncoding]];NSLog(%%%,topStr,data,bottomStr);// 1. RequestNSMutableURLRequest *request [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:20.0f];request.HTTPBody dataM;request.HTTPMethod “POST”;NSString *strLength [NSString stringWithFormat:%ld, (long)dataM.length];[request setValue:strLength forHTTPHeaderField:“Content-Length”];NSString *strContentType [NSString stringWithFormat:“multipart/form-data;boundary%”, randomIDStr];[request setValue:strContentType forHTTPHeaderField:“Content-Type”];[NSURLConnection sendAsynchronousRequest:requestqueue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse*response, NSData *data, NSError *connectionError) {NSString *result [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSLog(%, result);}];}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/915697.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!