gpt4 book ai didi

Swift:CoreData 以 90 度加载我的图像(肖像)

转载 作者:行者123 更新时间:2023-11-30 14:03:33 27 4
gpt4 key购买 nike

当我保存图像到 coreData 时,当我从 CoreData 重新打开它时,所有纵向拍摄的图像都是横向的。

我资助了很多关于它的预览问题,但都是在 Objective C 中而不是在 Swift 中。

如何解决这个问题?

这是我的代码:(它也是一个文本应用程序,当它工作时我会将它添加到我的项目中)

此文本应用程序有两个 ImageView ,一个用于从库加载,一个用于从 coreData 加载。

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

var monimage: String!

let imagePicker = UIImagePickerController()

@IBOutlet weak var MaPhoto: UIImageView? = UIImageView()
@IBOutlet weak var maPhoto2: UIImageView! = UIImageView()

var cameraUI:UIImagePickerController = UIImagePickerController()
var yourContacts:NSMutableArray = NSMutableArray()

override func viewDidLoad() {

imagePicker.delegate = self

super.viewDidLoad()

}

@IBAction func LabraryImage(sender: AnyObject) {

imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = true

presentViewController(imagePicker, animated: true, completion: nil)

}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func takePhoto(sender: UIButton) {

if (UIImagePickerController.isSourceTypeAvailable(.Camera)){

cameraUI = UIImagePickerController()
cameraUI.delegate = self
cameraUI.sourceType = UIImagePickerControllerSourceType.Camera
cameraUI.allowsEditing = true

self.presentViewController(cameraUI, animated: true, completion: nil)

}else{
//no camera available
let alert = UIAlertController(title: "Error", message: "There is no camera available", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Okay", style: .Default, handler: {(alertAction)in
alert.dismissViewControllerAnimated (true, completion: nil)
}))
}

}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
MaPhoto!.contentMode = .ScaleAspectFit
MaPhoto!.image = pickedImage
}

dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func btnSavePressed(sender : AnyObject) {

let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!

let ent = NSEntityDescription.entityForName("ImageData", inManagedObjectContext: context)

var newUser = ImageData (entity: ent!, insertIntoManagedObjectContext: context)

let contactImageData:NSData = UIImagePNGRepresentation(MaPhoto!.image)

newUser.monimage = contactImageData

context.save(nil)
self.navigationController?.popViewControllerAnimated(true)
}

@IBAction func loadImage(sender: AnyObject){

let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!

let request2 = NSFetchRequest (entityName: "ImageData")
request2.returnsObjectsAsFaults = false;

var results2:NSArray = context.executeFetchRequest(request2, error: nil)!

if results2.count > 0 {
for user in results2{
var thisUser2 = user as! ImageData


let profileImage:UIImage = UIImage(data: thisUser2.monimage)!

maPhoto2.image = profileImage

}
}

}

我还努力获取图像正方形,因此它是为了“allowEditing is = true”

最佳答案

感谢您的帮助!

这是我问题的答案:

@IBAction func btnSavePressed(sender : AnyObject) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
    let entity = NSEntityDescription.entityForName("ImageData",
inManagedObjectContext: managedContext)
let options = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext:managedContext)

var newImageData = UIImageJPEGRepresentation(MaPhoto!.image,1)

options.setValue(newImageData, forKey: "monimage")

var error: NSError?
managedContext.save(&error)

}

关于Swift:CoreData 以 90 度加载我的图像(肖像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32682373/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com