0
目前我正在做一個iOS應用程序,允許用戶上傳文件或圖像,但我沒有
想法如何開始。我需要某種在線服務器,可以讓我暫時保留
的上傳文件或圖像,並可以恢復。任何簡單的例子可以推薦?建議圖像和文件上傳爲iOS開發的示例
目前我正在做一個iOS應用程序,允許用戶上傳文件或圖像,但我沒有
想法如何開始。我需要某種在線服務器,可以讓我暫時保留
的上傳文件或圖像,並可以恢復。任何簡單的例子可以推薦?建議圖像和文件上傳爲iOS開發的示例
您可以使用Base64上傳圖片。首先,您必須將Base64從here添加到您的項目中。我在這裏添加了一個示例代碼。
- (void) uploadImage {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mywebpage.com/upload.json"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1000.0];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/design%i.png", docDir, designNum];
NSLog(@"%@",path);
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:path]);
[Base64 initialize];
NSString *imageString = [Base64 encode:imageData];
NSUInteger length = [imageString length];
NSUInteger chunkSize = 1000;
NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
NSString *chunk = [imageString substringWithRange:NSMakeRange(offset, thisChunkSize)];
offset += thisChunkSize;
NSArray *keys = [NSArray arrayWithObjects:@"design",@"design_id",@"fragment_id",nil];
NSArray *objects = [NSArray arrayWithObjects:chunk,@"design_id",[NSString stringWithFormat:@"%i", chunkNum],nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:kNilOptions error:&error];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d",[jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
[self startAsyncLoad:request withTag:[NSString stringWithFormat:@"tag%i",chunkNum]];
if (offset > length) {
stopSending = YES;
}
}
您也可以參考這個: