简体   繁体   English

从我的iPhone画廊上传照片

[英]upload photo from my iphone gallery

I am working on an application that needs its user to upload a photo for him, using iphone, how can i implement this : when click on the image in my personal information page, the gallery is opend to choose a photo. 我正在开发一个需要用户使用iphone为他上传照片的应用程序,我该如何实现:单击个人信息页面中的图像时,会打开图库以选择照片。 when the photo is chosen it must appear instead of the previous one (at first it is the default pic). 选择照片后,它必须出现而不是上一张(起初是默认图片)。 Notice that, the path of this photo or the photo itself(i do not know) must be stored in the DataBase for the next login. 请注意,此照片的路径或照片本身(我不知道)必须存储在数据库中,以便下次登录。 Moreover, this photo must be transferred to the server in order to be shown on the website when the same user login on the web. 此外,该照片必须传输到服务器,以便当同一用户登录到网络时在网站上显示。

I did it using the following code, i can choose a photo but when i save the path i could not load the pic again. 我使用以下代码完成了操作,我可以选择一张照片,但是当我保存路径时,无法再次加载图片。 it is a white box !! 这是一个白盒子! there is no photo !! 没有照片!

-(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];


}

now to load the pic : 现在加载图片:

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

Any Help will be appreciated.! 任何帮助将不胜感激。!

UIImagePickerControllerReferenceURL will return reference to local storage of your selected image. UIImagePickerControllerReferenceURL将返回对所选图像的本地存储的引用。

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

Now you can save this url in your database. 现在,您可以将该URL保存在数据库中。

To retrieve the image : display image from URL retrieved from ALAsset in iPhone 检索图像: 显示从iPhone中的ALAsset检索的URL中的图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM