iOS 업로드 이미지 (base64 및 파일 형식)

2847 단어

1. base64 이미지 업로드


 *   

 *

 *  @param image

 *  @param photoID

 *  @param photoType

 */

- (void)uploadPhotoToSrver:(UIImage *) image withPhotoID:(NSString *)photoID andPhotoType:(PhotoType)type andImageText:(NSString *)text {

    static int upLoadCount = 0;

    NSData *imageData = UIImageJPEGRepresentation(scaledImg, 0.6); 

    NSString *dataStr = [imageData base64EncodedStringWithOptions:0];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/xml"];

    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

// 

    NSDictionary *parameters = @{@"UserID" : userInfo.UserID,  @"imageDataStr": dataStr};

// url String

    NSString *baseURL = [NSString stringWithFormat: @"%@%@", WEBBASEURL, WEBUploadPhotoDataAction];



    [manager POST:baseURL parameters:parameters success:^(AFHTTPRequestOperation *operation,id responseObject) {

        NSLog(@" ");

    }failure:^(AFHTTPRequestOperation *operation,NSError *error) {

        MYNSLOG(@"~~~~~~~~%@",error);

    }];

    

}

2. 파일 업로드 이미지 (서버의 [file] 유형에 해당)


- (void)personalUploadHeadImgInterfaceWithImage:(UIImage *)uploadImage  {

//  

    NSDictionary *parameterDict = @{@"userkey" : appDelegate.userKeyString};

    

    // 

    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];

    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

    NSString *requestUrlStr = [NSString stringWithFormat:@"%@%@", WEBBASEURL,  WEBInterFace_Personal_UploadHeadImg];

    [sessionManager POST:requestUrlStr parameters:parameterDict constructingBodyWithBlock:^(id  _Nonnull formData) {

        //     [file]

        NSData *imageData = UIImageJPEGRepresentation(uploadImage, imageType == 1);

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        formatter.dateFormat = @"yyyyMMddHHmmss";

        NSString *str = [formatter stringFromDate:[NSDate date]];

        NSString *fileName = [NSString stringWithFormat:@"%@%@.jpg", appDelegate.userKeyString, str];

        

        [formData appendPartWithFileData:imageData name:@"headImg"  fileName:fileName mimeType:@"image/jpg"];       //  key

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

        MYLOG(@"success");



    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        MYLOG(@"failure:%@", error);

    }];

}

좋은 웹페이지 즐겨찾기