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对象。能有更直接的方法吗?.. –