ios-上传图片到后台
做第一个项目时,有个版块的个人信息的编辑涉及到头像修改,老大说项目里有通用的代码,让我自己去找。总算找到,搞了许久才弄好,看来理解能力还需要提高啊!!
1 #pragma mark- 修改头像上传后保存 2 -(void)uploadMyIco 3 { 4 //NSString *fileName = @"myico.jpg"; 5 UIImage *image1 = self.headImageVie.image; 6 7 NSData *data; 8 float v = image1.size.width / 240; 9 CGSize newSize = CGSizeMake(image1.size.width / v, image1.size.height / v); 10 UIGraphicsBeginImageContext(newSize); 11 [image1 drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 12 UIImage *scaleImage = UIGraphicsGetImageFromCurrentImageContext(); 13 UIGraphicsEndImageContext(); 14 data = UIImageJPEGRepresentation(scaleImage, 0.8); 15 UIImage *image = [UIImage imageWithData:data]; 16 UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil ); 17 NSString *fileNamess =[self createImgFileName]; 18 NSString* filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileNamess]; 19 [data writeToFile:filePath atomically:NO]; 20 NSMutableArray *imageArray = [[NSMutableArray alloc]init]; 21 [imageArray addObject:[NSDictionary dictionaryWithObject:filePath forKey:@"file"]]; 22 self.request = nil; 23 requestType = 2; 24 self.request = [UPRequest uploadPhotoInfo1:@"member/updatePhoto" image:imageArray keyName:@"file" delegate:self params:nil]; 25 [SKProgressView ShowHudInView:self.view withTag:2001]; 26 } 27 28 - (NSString *)createImgFileName 29 { 30 static int seedNum = 0; 31 if(seedNum >= 1000) 32 seedNum = 0; 33 seedNum++; 34 35 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 36 [dateFormatter setDateFormat:@"yyyyMMddHHmmss"]; 37 38 NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]]; 39 // [dateFormatter release]; 40 41 NSString *file = [NSString stringWithFormat:@"temppics%@%03d.jpg", currentDateStr, seedNum]; 42 return file; 43 }
部门logo修改是是参照写的〜
1 #pragma mark- 图像上传保存 2 - (void)uploadDepartmentLogo//上传部门logo图片 3 { 4 NSString *fileName = @"departMentLogo.jpg"; 5 UIImage *image1 = self.headImageView.image; 6 NSData *data; 7 if ( UIImagePNGRepresentation(image1) == nil ) 8 { 9 data = UIImageJPEGRepresentation(image1, 1); 10 fileName = @"departMentLogo.png"; 11 } else { 12 data = UIImagePNGRepresentation(image1); 13 fileName = @"departMentLogo.jpg"; 14 } 15 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 16 NSString *documentsDirectory = [paths objectAtIndex:0]; 17 NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:fileName]; 18 [data writeToFile:fullPathToFile atomically:NO]; 19 20 NSMutableArray *array = [NSMutableArray arrayWithCapacity:0]; 21 [array addObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:1] forKey:@"type"]]; 22 [array addObject:[NSDictionary dictionaryWithObject:self.deptId forKey:@"deptId"]]; 23 24 25 NSMutableArray *imageArray = [[NSMutableArray alloc]init]; 26 [imageArray addObject:[NSDictionary dictionaryWithObject:fullPathToFile forKey:@"file"]];//deptHead 27 28 self.request = nil; 29 requestType = 2; 30 self.request = [UPRequest uploadPhotoInfo1:@"dept/uploadPhoto" image:imageArray keyName:@"file" delegate:self params:array]; 31 }