我想在一个viewcontroller上显示两个图像,从相机胶卷中选择它们,
我已经设法进入相机胶卷以便能够选择它们但是当我选择一个图像时,它将它们放在两个UIImageViews中。
我希望能够在UIImageView中显示两个不同的图像。
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//handle media here i.e. do stuff with photo
print("imagePickerController called")
if let chosenImage = info[UIImagePickerControllerEditedImage] {
profileImage.image = chosenImage as? UIImage
let user: PFUser = PFUser.currentUser()!
let profileImageData = UIImageJPEGRepresentation(profileImage.image!, 1)
if (profileImageData != nil)
{
let profileFileObject = PFFile(data: profileImageData!)
user.setObject(profileFileObject!, forKey: "profilePicture")
}
}
到此为止我可以选择一个图像并将其保存到Parse成功,但是当我尝试为其他UIImageView添加其他代码时,如下所示:
if let chosenCoverImage = info[UIImagePickerControllerEditedImage] {
coverImage.image = chosenCoverImage as? UIImage
let coverUser: PFUser = PFUser.currentUser()!
let coverImageData = UIImageJPEGRepresentation(coverImage.image!, 1)
if (coverImageData != nil)
{
let coverFileObject = PFFile(data: coverImageData!)
coverUser.setObject(coverFileObject!, forKey: "coverPicture")
}}
}
代码的第二部分似乎不起作用,基本上它在UIImageViews中显示相同的图像,我认为我需要的是第二部分
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
}
但在同一个快速文件中,这显然是不可能的。
关于如何实现这一目标的任何想法?
提前谢谢!
这是我的完整imagePickerConytoller代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//handle media here i.e. do stuff with photo
print("imagePickerController called")
let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
profileImage.image = chosenImage
let user: PFUser = PFUser.currentUser()!
let profileImageData = UIImageJPEGRepresentation(profileImage.image!, 1)
if (profileImageData != nil)
{
let profileFileObject = PFFile(data: profileImageData!)
user.setObject(profileFileObject!, forKey: "profilePicture")
}
let chosenCoverImage = info[UIImagePickerControllerEditedImage] as? UIImage
coverImage.image = chosenCoverImage
let coverUser: PFUser = PFUser.currentUser()!
let coverImageData = UIImageJPEGRepresentation(coverImage.image!, 1)
if (coverImageData != nil)
{
let coverFileObject = PFFile(data: coverImageData!)
coverUser.setObject(coverFileObject!, forKey: "coverPicture")
}
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
activityIndicator.center = self.profileImage.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.White
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
activityIndicator.color = UIColor.grayColor()
user.saveInBackgroundWithBlock { (success:Bool, error:NSError?) -> Void in
self.activityIndicator.stopAnimating()
}
dismissViewControllerAnimated(true, completion: nil)
}
以下是我在ViewDidLoad中从Parse获取它们的方法:
if (PFUser.currentUser()?.objectForKey("profilePicture") != nil)
{
let userImageFile:PFFile = PFUser.currentUser()?.objectForKey("profilePicture") as! PFFile
userImageFile.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
self.profileImage.image = UIImage(data: imageData!)
})
}
if (PFUser.currentUser()?.objectForKey("coverPicture") != nil)
{
let userCoverFile:PFFile = PFUser.currentUser()?.objectForKey("coverPicture") as! PFFile
userCoverFile.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
self.coverImage.image = UIImage(data: imageData!)
})
}
答案 0 :(得分:2)
这不是完整的,但会有你需要的关键部分
class ResponsePhotoController: UIViewController, UIImagePickerControllerDelegate
{
var imageType : Int = 0 // use this to track the image type
// 0 for profile
// 1 for cover
在您的代码中获取个人资料图片
imageType = 0
然后当你去获得封面图片
imageType = 1
然后在imagePickerController
中if imageType == 0
{
// set up the profile picture
}
else
{
// set up the cover image
}
答案 1 :(得分:0)
类ResponsePhotoController:UIViewController,UIImagePickerControllerDelegate { imageType:Int = 0 //使用它来查找图像类型公共类变量
if CameraFor == "ImageForProfile" {
//set image here for profile
}
else {
//set image here for ImageForCoverPhoto
}
}