Ios 后台图像上传
您好,在我的应用程序中,我需要通过将UIImage编码为Base64string在后台上载多个图像。请让我知道如何继续执行相同的操作,或者建议我一些更好的方法 无论如何,先谢谢你 问候 塔帕什注:见编辑1 你可以试试这样的Ios 后台图像上传,ios,objective-c,iphone,ipad,Ios,Objective C,Iphone,Ipad,您好,在我的应用程序中,我需要通过将UIImage编码为Base64string在后台上载多个图像。请让我知道如何继续执行相同的操作,或者建议我一些更好的方法 无论如何,先谢谢你 问候 塔帕什注:见编辑1 你可以试试这样的 NSArray* images = // Get your images [self performSelectorInBackground:@selector(uploadImages:) withObject:images]; ... - (void)uploadIm
NSArray* images = // Get your images
[self performSelectorInBackground:@selector(uploadImages:) withObject:images];
...
- (void)uploadImages:(NSArray*)images {
// Encode images and upload them...
}
performSelectorInBackground:withObject在后台线程中执行该方法。在本例中,uploadImages:方法应在同一类中定义,或委托给Util类或类似类
编辑1
正如@user1963877所说,performSelectorInBackground:withObject已经过时,并逐渐被替换,因此不应再使用它。使用dispatch_async,您可以复制相同的行为,如下所示:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// Encode images and upload them, you are in a background thread...
dispatch_async(dispatch_get_main_queue(), ^{
// Do whatever in UI, you are in UI thread
}
}
我终于实现了。程序如下所示。我要求所有人在需要时对此进行修改,并提出更有效的方法:
typealias PLMImageUploadCompletionHandler = (AnyObject?, Int, NSError?) -> Void
class ImageUploader: NSObject {
static let sharedInstance = ImageUploader()
private override init() {} //This prevents others from using the default '()' initializer for this class.
//MARK:- Api Calls
func uploadImageWithAlamofire(selectedImages: Array<Image> , completionHandler :@escaping PLMImageUploadCompletionHandler)
{
//***** Added By Priti ***********////
let username = "test"
let password = "XXX122"
let loginString = String(format: "%@:%@", username, password)
let headers:HTTPHeaders = [
"Authorization": "Basic \(loginString.data(using: String.Encoding.utf8)!.base64EncodedString())"
]
let imageServiceURl = URL(string:"your url")
let postParams:[String: Any] = [
"fileName":String(describing: fileArray),
"objectID":String(describing: objectID),
"comments":String(describing: commentsArray),
"objectType":String(describing: objectType),
"uniqueId":String(describing: arrIDs),
"location":String(describing: locationArray)
]
print(fileArray)
Alamofire.upload(multipartFormData: { multipartFormData in
for (key, value) in postParams
{
multipartFormData.append(((value as AnyObject).data(using:String.Encoding.utf8.rawValue))!, withName: key)
}
for i in 0..<selectedImages.count {
let data = UIImageJPEGRepresentation(selectedImages[i].value(forKey: "image") as! UIImage,0.6)
multipartFormData.append(data!, withName: "uploadedFiles", fileName: "image\(i).png", mimeType: "image/png")
}
},to:imageServiceURl!, method: .post, headers:headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON {
response in
switch response.result
{
case.success:
print(response.request!) // original URL request
print(response.response!) // URL response
print(response.data!) // server data
print(response.result) // result of response serialization
// Post notification
DispatchQueue.main.async( execute: {
completionHandler(nil, HttpStatusType.OK_STATUS. rawValue, nil)
})
break
case.failure(let error):
completionHandler(nil, HttpStatusType.HTTP_ERROR. rawValue, error as NSError)
break
}
}
case .failure(let encodingError):
print("error:\(encodingError)")
}
})
}
}
为什么要对图像进行编码…您想一个接一个或一次上载所有图像?自从引入GCD以来,performSelectorInBackground已过时。最好使用dispatch_async。@user1963877查看我的编辑,如果确定,您可以撤消DownVoteTank以获得答案。请让我知道在使用GCD上传图像时,当我的应用程序进入后台时会发生什么。您在问题中没有指定进入后台的是应用程序本身,而只是线程。看看