我正在开发一个需要用户为他上传照片的应用程序,使用iphone,我该如何实现这一点:当点击我个人信息页面中的图像时,图库会打开以选择照片。当选择照片时,它必须出现而不是前一张照片(首先它是默认的照片)。请注意,此照片的路径或照片本身(我不知道)必须存储在DataBase中以便下次登录。此外,必须将此照片传输到服务器,以便在同一用户登录网站时显示在网站上。
我使用以下代码完成,我可以选择一张照片,但是当我保存路径时,我无法再次加载图片。这是一个白色的盒子!没有照片!!
-(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];
}
现在加载pic:
UIImage *img;
if(pic != nil)
img=[UIImage imageWithContentsOfFile:pic];
else
img= [UIImage imageNamed:@"business_userL.png"];
任何帮助将不胜感激。!
答案 0 :(得分:1)
UIImagePickerControllerReferenceURL
将返回对所选图片的本地存储空间的引用。
NSURL* localUrl = (NSURL *)[info valueForKey:UIImagePickerControllerReferenceURL];
现在您可以将此网址保存在数据库中。
要检索图像:display image from URL retrieved from ALAsset in iPhone