iOS 上传壁纸
项目
- 在项目中我是使用第二篇稳重中的方法,但是具体我还没实现,需要请教大佬
@implementation WoWUpWallpaperViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"上传壁纸";
// 点击UIButton AddWallpaper 是打开相册添加图片
[_AddWallpaper addTarget:self action:@selector(AddWallpaperClick) forControlEvents:UIControlEventTouchUpInside];
// 点击UIButton UpWallPaper 是上传图片
[_UpWallpaper addTarget:self action:@selector(UpWallpaperClick) forControlEvents:UIControlEventTouchUpInside];
}
# pragma mark -- 添加壁纸
- (void)AddWallpaperClick {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"请选择图片" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self openPhotoAlbum];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:OKAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)UpWallpaperClick {
QWLog(@" 上传成功啦~~~~~~~ \n成功了?\n\t好像没有\n\t\t真的没有?\n\t\t\temmmmm。。。。 ");
[self postDataWithUrl:@"http://www.qiangwan.com/index.php?tp=up&op=image" params:nil imageDatas:nil success:^(id response) {
[QWShowMessage showToastmsg:@"上传壁纸成功"];
} failure:^(NSError *error) {
[QWShowMessage showToastmsg:@"上传壁纸失败"];
}];
}
- (void)openPhotoAlbum {
// 1.判断相册是否可以打开
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
// 2. 创建图片选择控制器
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
/**
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary, // 相册
UIImagePickerControllerSourceTypeCamera, // 用相机拍摄获取
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿
}
*/
// 3. 设置打开照片相册类型(显示所有相簿)
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// 照相机
// ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
// 4.设置代理
ipc.delegate = self;
// 5.modal出这个控制器
[self presentViewController:ipc animated:YES completion:nil];
}
#pragma mark -- <UIImagePickerControllerDelegate>--
// 获取图片后的操作
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
// 销毁控制器
[picker dismissViewControllerAnimated:YES completion:nil];
// 设置图片
_UpImage.image = info[UIImagePickerControllerOriginalImage];
}
#pragma mark --- 上传图片
- (void)postDataWithUrl:(NSString *)url params:(NSMutableDictionary *)params imageDatas:(NSArray *)images success:(void (^)(id response))success failure:(void (^)(NSError *error))failure
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json",@"text/html",@"text/javascript",@"text/xml",@"text/plain",nil]];
[manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
for (UIImage *image in images) {
// 图片太大会上传不到服务器上面去
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);;
// 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名
// 要解决此问题,
// 可以在上传时使用当前的系统事件作为文件名
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置时间格式
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
/*
此方法参数
1. 要上传的[二进制数据]
2. 对应网站上[upload.php中]处理文件的[字段"file"]
3. 要保存在服务器上的[文件名]
4. 上传文件的[mimeType]
*/
NSUInteger index = [images indexOfObject:image];
if (index == images.count-1) {
// 上传位置图片data
[formData appendPartWithFileData:imageData name:@"posfile" fileName:fileName mimeType:@"image/png"];
} else {
[formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"];
}
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"uploadProgress = %@",uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
}