私はユーザーが写真にタイムスタンプを追加できるようにするアプリを開発中です。カメラのロールからの写真が選択されると、私のアプリがクラッシュすることがあり、Xcodeは「デバッガからのメッセージ:メモリの問題により終了しました」というエラーを表示します。非常に大きな画像にタイムスタンプを追加しようとした場合、または同じ画像に対してタイムスタンプを繰り返しているとクラッシュしていました。変更された画像をカメラロールに保存するとメモリの問題で終了しました
以下は、この問題に関連すると思われるコードです。また、hereはプロジェクト全体へのリンクです。
タイムスタンプがイメージに追加される方法です。私はそれがクラッシュするコメントを入れます。同じコード行で必ずしもクラッシュするとは限りません。なぜ私のコードは、メモリの問題のエラーでクラッシュさせているのか分かりません。
func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
let color = UserDefaults.standard.colorForKey(key: "color")!
let font = UserDefaults.standard.value(forKey: "font") as! String
let size = UserDefaults.standard.value(forKey: "size") as! Int
let fontAndSize = UIFont(name: font, size: CGFloat(size))!
let location = Locations(rawValue: UserDefaults.standard.value(forKey: "location") as! String)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
// sometimes terminates here
let textFontAttributes = [
NSFontAttributeName: fontAndSize,
NSForegroundColorAttributeName: color
] as [String : Any]
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
let rectSize = text.boundingRect(with: CGSize(width:CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: textFontAttributes, context: nil).size
// sometimes terminates here
if location == .topRight || location == .bottomRight || location == .center || location == .topCenter || location == .bottomCenter {
// Calculate the text size
if location == .center || location == .topCenter || location == .bottomCenter {
// Subtract half the text width from the x origin
let rect = CGRect(x:point.x-(rectSize.width/2), y:point.y-(rectSize.height/2), width:rectSize.width, height: rectSize.height)
text.draw(in: rect, withAttributes: textFontAttributes)
} else {
// Subtract the text width from the x origin
let rect = CGRect(x:point.x-rectSize.width, y:point.y-(rectSize.height/2), width:rectSize.width, height: rectSize.height)
text.draw(in: rect, withAttributes: textFontAttributes)
}
} else {
let rect = CGRect(x:point.x, y:point.y-(rectSize.height/2), width:rectSize.width, height: rectSize.height)
text.draw(in: rect, withAttributes: textFontAttributes)
}
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
func selectPhotoFromCameraRoll(mediaType: String) {
imagePicker.sourceType = .photoLibrary
if mediaType == "Video" {
imagePicker.mediaTypes = [kUTTypeMovie as String]
}
newMedia = false
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let mediaType = info[UIImagePickerControllerMediaType] as? String {
if mediaType.isEqual((kUTTypeImage as String)) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let width = pickedImage.size.width
let height = pickedImage.size.height
let location = Locations(rawValue: UserDefaults.standard.value(forKey: "location") as! String)!
var point = CGPoint(x: 0, y: 0)
switch location {
case .topLeft:
point = CGPoint(x: 30, y: 50)
case .topRight:
point = CGPoint(x: width - 30, y: 50)
case .bottomLeft:
point = CGPoint(x: 30, y: height - 50)
case .bottomRight:
point = CGPoint(x: width - 30, y: height - 50)
case .center:
point = CGPoint(x: width/2, y: height/2)
case .topCenter:
point = CGPoint(x: width/2, y: 50)
case .bottomCenter:
point = CGPoint(x: width/2, y: height - 50)
}
let savedFormat = UserDefaults.standard.value(forKey: "format") as! String
var date = Date()
if !currentDateBool {
date = UserDefaults.standard.value(forKey: "selectedDate") as! Date
}
let timestampText = getFormattedDateFromFormatType(formatType: savedFormat, date: date) as NSString
let timestampImage = textToImage(drawText: timestampText, inImage: pickedImage, atPoint: point)
if newMedia {
UIImageWriteToSavedPhotosAlbum(timestampImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
} else {
UIImageWriteToSavedPhotosAlbum(timestampImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
}
} else if mediaType.isEqual((kUTTypeMovie as String)) {
if let videoUrl = info[UIImagePickerControllerMediaURL] as? NSURL {
if let videoPath = videoUrl.relativePath {
if newMedia {
UISaveVideoAtPathToSavedPhotosAlbum(videoPath, nil, nil, nil)
}
}
}
}
}
dismiss(animated: true, completion: nil)
}
func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
ありがとうございました!あなたはイメージをどのように扱うべきかに関する勧告を持っていますか? – chickenparm
ありがとうございます。私はそれに多くの経験がないので、写真フレームワークを調べます。あなたは正しい方向で私を指摘し、私はUIGraphicsBeginImageContextWithOptionsを全く使用すべきでないかどうか教えていただけますか? – chickenparm
申し訳ありませんが、私は私の質問に不明確です。ユーザーはカメラロールに既存の写真があります。ユーザーは写真に日付/時刻のテキストを追加したいと考えています。私が変更したいのは、写真にテキストを追加することだけです。上のコードはカメラロールを開き、ユーザーが写真を選択して写真にテキストを追加し、その写真の日付/時刻のテキストを含むカメラロールにその写真の新しいバージョンを保存します。 – chickenparm