Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
galaxy-iOS
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mobile-group
galaxy-iOS
Commits
b96c7173
Commit
b96c7173
authored
Feb 27, 2024
by
pete谢兆麟
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
upload 封装
parent
47c64236
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
101 additions
and
32 deletions
+101
-32
YHImagePickerView.swift
...ainApplicantInformation(主申请人信息)/V/YHImagePickerView.swift
+69
-31
YHMainInformationCardTableViewCell.swift
...mation(主申请人信息)/V/YHMainInformationCardTableViewCell.swift
+1
-1
YHNetRequest.swift
galaxy/galaxy/Classes/Tools/NetWork/YHNetRequest.swift
+31
-0
No files found.
galaxy/galaxy/Classes/Modules/IntelligentService(服务中心)/ServiceProcess(流程)/MainApplicantInformation(主申请人信息)/V/YHImagePickerView.swift
View file @
b96c7173
...
...
@@ -206,37 +206,7 @@ extension YHImagePickerView: UIGestureRecognizerDelegate {
extension
YHImagePickerView
:
UINavigationControllerDelegate
,
UIImagePickerControllerDelegate
{
func
imagePickerController
(
_
picker
:
UIImagePickerController
,
didFinishPickingMediaWithInfo
info
:
[
UIImagePickerController
.
InfoKey
:
Any
])
{
if
let
image
=
info
[
UIImagePickerController
.
InfoKey
.
originalImage
]
as?
UIImage
{
// 将图片显示给UIImageView
// if let block = backImage {
// block(image)
// self.dismiss()
// }
//将选择的图片保存到Document目录下
let
fileManager
=
FileManager
.
default
let
rootPath
=
NSSearchPathForDirectoriesInDomains
(
.
documentDirectory
,
.
userDomainMask
,
true
)[
0
]
as
String
let
filePath
=
"
\(
rootPath
)
/pickedimage.jpg"
let
imageData
=
image
.
jpegData
(
compressionQuality
:
1.0
)
fileManager
.
createFile
(
atPath
:
filePath
,
contents
:
imageData
,
attributes
:
nil
)
//上传图片
if
(
fileManager
.
fileExists
(
atPath
:
filePath
)){
//取得NSURL
let
imageURL
=
URL
(
fileURLWithPath
:
filePath
)
//使用Alamofire上传
AF
.
upload
(
imageURL
,
to
:
"https://test-comserver.galaxy-immi.com/oss/upload/storage"
)
.
responseString
{
response
in
switch
response
.
result
{
case
.
success
(
let
value
):
print
(
"Success:
\(
value
)
"
)
case
.
failure
(
let
error
):
break
}
}
}
uploadImage
(
image
:
image
)
}
else
{
printLog
(
"pick image wrong"
)
}
...
...
@@ -248,5 +218,73 @@ extension YHImagePickerView: UINavigationControllerDelegate, UIImagePickerContro
}
//原生上传
func
uploadImage
(
imageURL
:
URL
)
{
let
url
=
URL
(
string
:
"https://test-comserver.galaxy-immi.com/oss/upload/storage"
)
!
// 设置上传接口地址
var
request
=
URLRequest
(
url
:
url
)
request
.
httpMethod
=
"POST"
do
{
let
imageData
=
try
Data
(
contentsOf
:
imageURL
)
// 读取要上传的图片数据
let
boundary
=
UUID
()
.
uuidString
// 生成随机边界字符串
request
.
setValue
(
"multipart/form-data; boundary=
\(
boundary
)
"
,
forHTTPHeaderField
:
"Content-Type"
)
request
.
addValue
(
"4001001"
,
forHTTPHeaderField
:
"businessCode"
)
var
body
=
""
body
+=
"--
\(
boundary
)\r\n
"
body
+=
"Content-Disposition: form-data; name=
\"
file
\"
; filename=
\"
image.jpg
\"\r\n
"
// 根据需求修改文件名称
body
+=
"Content-Type: image/jpeg
\r\n\r\n
"
// 根据实际情况指定正确的MIME类型
if
let
data
=
imageData
.
base64EncodedString
(
options
:
.
lineLength76Characters
)
.
addingPercentEncoding
(
withAllowedCharacters
:
CharacterSet
.
alphanumerics
)
{
let
encodedBody
=
body
+
"
\r\n
"
+
data
+
"
\r\n
"
+
"--"
+
boundary
+
"--"
guard
let
postData
=
encodedBody
.
data
(
using
:
String
.
Encoding
.
utf8
)
else
{
return
}
request
.
httpBody
=
postData
let
task
=
URLSession
.
shared
.
dataTask
(
with
:
request
)
{
(
data
,
response
,
error
)
in
if
let
error
=
error
{
print
(
"Error:
\(
error
)
"
)
}
else
if
let
httpResponse
=
response
as?
HTTPURLResponse
{
switch
httpResponse
.
statusCode
{
case
200
..<
300
:
if
let
data
=
data
{
let
string
=
String
(
data
:
data
,
encoding
:
.
utf8
)
}
print
(
"Upload successful."
)
default
:
print
(
"Failed to upload the image with status code:
\(
httpResponse
.
statusCode
)
"
)
}
}
}
task
.
resume
()
}
}
catch
{
print
(
"Error reading image file:
\(
error
)
"
)
}
}
func
uploadImage
(
image
:
UIImage
)
{
// 获取图片的二进制数据
guard
let
imageData
=
image
.
jpegData
(
compressionQuality
:
1
)
else
{
return
}
let
boundary
=
UUID
()
.
uuidString
let
headers
:
HTTPHeaders
=
[
"Content-type"
:
"multipart/form-data; boundary=
\(
boundary
)
"
,
"businessCode"
:
"4001001"
]
// 创建MultipartFormData对象
var
multipartFormData
=
MultipartFormData
()
// 将图片添加到multipartFormData中
multipartFormData
.
append
(
imageData
,
withName
:
"file"
,
fileName
:
"image.jpg"
,
mimeType
:
"image/jpeg"
)
// 发送POST请求
let
uploadRequest
=
AF
.
upload
(
multipartFormData
:
multipartFormData
,
to
:
"https://test-comserver.galaxy-immi.com/oss/upload/storage"
,
headers
:
headers
)
uploadRequest
.
responseString
(
completionHandler
:
{
string
in
printLog
(
"
\n
网络请求已返回 string=
\(
string
)
"
)
})
}
}
galaxy/galaxy/Classes/Modules/IntelligentService(服务中心)/ServiceProcess(流程)/MainApplicantInformation(主申请人信息)/V/YHMainInformationCardTableViewCell.swift
View file @
b96c7173
...
...
@@ -133,7 +133,7 @@ class YHMainInformationCardTableViewCell: UITableViewCell {
frontLabel
=
{
let
label
=
UILabel
()
label
.
font
=
UIFont
.
PFSC_R
(
ofSize
:
12
)
label
.
textColor
=
UIColor
.
contentBkg
Color
label
.
textColor
=
UIColor
.
subText
Color
label
.
textAlignment
=
.
center
return
label
}()
...
...
galaxy/galaxy/Classes/Tools/NetWork/YHNetRequest.swift
View file @
b96c7173
...
...
@@ -161,6 +161,32 @@ class YHNetRequest: NSObject {
return
httpRequest
}
///开始请求
func
uploadRequest
(
_
headers
:
HTTPHeaders
,
_
image
:
UIImage
)
->
Request
?
{
guard
YHNetworkStatusManager
.
shared
.
isNetWorkOK
==
true
else
{
YHHUD
.
flash
(
message
:
"网络异常"
)
return
httpRequest
}
guard
let
imageData
=
image
.
jpegData
(
compressionQuality
:
1
)
else
{
return
httpRequest
}
var
multipartFormData
=
MultipartFormData
()
// 将图片添加到multipartFormData中
multipartFormData
.
append
(
imageData
,
withName
:
"file"
,
fileName
:
"image.jpg"
,
mimeType
:
"image/jpeg"
)
// 发送POST请求
let
uploadRequest
=
AF
.
upload
(
multipartFormData
:
multipartFormData
,
to
:
"https://test-comserver.galaxy-immi.com/oss/upload/storage"
,
headers
:
headers
)
uploadRequest
.
responseString
(
completionHandler
:
{
string
in
#if DEBUG
printLog
(
"
\n
网络请求已返回 string=
\(
string
)
"
)
#endif
self
.
completeHandle
(
string
:
string
)
})
return
uploadRequest
}
/// 请求response完成处理
func
completeHandle
(
string
:
AFDataResponse
<
String
>
)
{
let
responseResult
=
string
.
result
...
...
@@ -250,4 +276,9 @@ extension YHNetRequest {
printLog
(
"
\n
发起请求的URL是===> url =
\(
url
)
"
)
return
YHNetRequest
()
.
url
(
url
)
.
requestType
(
.
post
)
.
params
(
params
)
.
success
(
successBlock
)
.
failed
(
failBlock
)
.
startRequest
()
}
class
func
uplaodRequest
(
url
:
String
,
headers
:
HTTPHeaders
,
image
:
UIImage
,
successBlock
:
SuccessHandlerType
!
,
failBlock
:
FailureHandlerType
!
)
->
Request
?
{
printLog
(
"
\n
发起请求的URL是===> url =
\(
url
)
"
)
return
YHNetRequest
()
.
url
(
url
)
.
success
(
successBlock
)
.
failed
(
failBlock
)
.
uploadRequest
(
headers
,
image
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment