【问题标题】:upload photo from my iphone gallery从我的 iPhone 画廊上传照片
【发布时间】:2013-11-17 19:23:05
【问题描述】:

我正在开发一个应用程序,该应用程序需要其用户使用 iphone 为他上传照片,我该如何实现:当单击我的个人信息页面中的图像时,会打开图库以选择照片。选择照片时,必须出现它而不是前一个(首先是默认图片)。请注意,这张照片的路径或照片本身(我不知道)必须存储在数据库中以便下次登录。此外,该照片必须传输到服务器,才能在同一用户登录网络时显示在网站上。

我使用以下代码完成了此操作,我可以选择一张照片,但是当我保存路径时,我无法再次加载图片。这是一个白盒子!没有照片!!

-(void) pickPhoto
{
    UIImagePickerController *picker ;
    picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    else
    {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
    [self presentViewController:picker animated:YES completion:nil];
}

-(void) imagePickerController:(UIImagePickerController *)picker    didFinishPickingMediaWithInfo:(NSDictionary *)info
{

UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];

if(!img)
    img = [info objectForKey:UIImagePickerControllerOriginalImage];


NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath =  [docDirPath stringByAppendingPathComponent:@"myImage.png"];
NSLog (@"File Path = %@", filePath);


UIButton *btn = (UIButton *)[self.tableView viewWithTag:2013];
[btn setBackgroundImage:img forState:UIControlStateNormal];

database= [[connectToDB alloc] init];
[database setDelegate:self];
[database saveUserPic:filePath ForUser:user.userId];
[self dismissViewControllerAnimated:YES completion:nil];


}

现在加载图片:

UIImage *img;
if(pic != nil)
     img=[UIImage imageWithContentsOfFile:pic];
else
     img= [UIImage imageNamed:@"business_userL.png"];

任何帮助将不胜感激。!

【问题讨论】:

  • UIImagePickerController 应该会有所帮助。你现在的代码是什么?
  • 您的所有尝试都是访问您 iPhone 的资产库。有两种方法可以实现: 1. 使用 UIImagePickerController,这是标准 UIKit 的一部分。 2. 使用 AssetLibrary.framework,需要明确添加到你的项目中。具体来说,如果您正在为您的任务寻找一个简单且系统提供的界面,那么 UIImagePickerController 应该是您的选择。另一方面,如果您需要完全控制和可自定义的界面,我建议您使用 AssetLibrary.framework。请自行进行一些研究以做出决定。
  • 我发现您在filePath 变量中生成了一个路径表达式并将其保存到数据库中。但我没有找到任何代码将实际图像保存在实际filePath 位置。您是否遗漏了几行代码?
  • 不,我尝试的是保存图像的路径而不是图像本身。但即便如此,我也无法从其路径加载图像!要使用网络服务发送照片,我必须保存实际照片吗?
  • @etab :是的,很明显你已经保存了你的路径。但是要从路径加载图像,您仍然需要图像在该路径中物理可用,这意味着您需要将图像保存在该路径中。我认为你只是错过了这一点。

标签: ios iphone objective-c


【解决方案1】:

UIImagePickerControllerReferenceURL 将返回对您所选图像的本地存储的引用。

NSURL* localUrl = (NSURL *)[info valueForKey:UIImagePickerControllerReferenceURL];

现在您可以将此网址保存在您的数据库中。

要检索图像:display image from URL retrieved from ALAsset in iPhone

【讨论】:

    猜你喜欢
    • 2015-03-11
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多