0
我試圖從我的應用上傳照片到Web服務。從NSURL獲取NSData
我試圖創建的流程如下:
- 用戶拍攝的照片與相機
- 照片是一個自定義的專輯下保存到相機膠捲
- URL保存照片的是給我的商店
- 存儲嘗試將照片上傳到Web服務。
我試圖使用NSData *data = [NSData dataWithContentsOfURL:[item assetURL]]
其中項目是一個包含有關URL的典範。但該行沒有生產數據,當我登錄它,即使它會產生一個網址:"assets-library://asset/asset.PNG?id=28DBC0AC-21FF-4560-A9D6-5F4BCA190BDB&ext=PNG"
的代碼片段如下:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:^(void){
[library writeImageToSavedPhotosAlbum:image.CGImage orientation:image.imageOrientation completionBlock:^(NSURL* assetURL, NSError* error) {
BCard *card = [[BCard alloc]init];
//error handling
if (error!=nil) {
NSLog(@"[ERROR] - %@",error);
return;
}
//add the asset to the custom photo album
[library addAssetURL: assetURL
toAlbum:@"Business Cards"
withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(@"Custom Album Error: %@", [error description]);
}
}];
[card setAssetURL:assetURL];
[[BCardStore sharedStore]addToQueue:card];
int index = [[[BCardStore sharedStore]getQueue]count]-1;
[[BCardStore sharedStore]uploadItemAtIndex:index withProgressBlock:nil withExitBlock:nil];
}];
}];
}
和
-(void)uploadItemAtIndex:(NSUInteger)index withProgressBlock:(progressBlock)pBlock withExitBlock:(exitBlock)eBlock
{
BCard *item = [uploadQueue objectAtIndex:index];
NSURL *url = [NSURL URLWithString:@"http://192.168.0.116:8080"];
NSData *data = [NSData dataWithContentsOfURL:[item assetURL]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
numberedName = numberedName +1;
NSString *name = [NSString stringWithFormat:@"%d",numberedName];
NSLog(@"%@",[item assetURL]);
//upload data using AFNetworking here
}
的片段[library addAssetUrl:NSUrl toAlbum:NSString withCompletionBlock:^(NSError *error)]
來了從我發現的類別here。
我真的在這裏找到正確的URL嗎?還是我錯誤地使用了dataWithContentsOfURL
?
所以我找到[這裏](http://stackoverflow.com/questions/5187251/ios-select-a-gif - 從照片庫轉換爲nsdata使用在多部分),同時戳動。它似乎添加了太多的代碼,我很關心內存,因爲我在上傳照片時會出現一個NSData和bytea對象。能有更直接的方法嗎?.. –