私は少し違う何かをした:私はあなたが複数の場所で使用できるクラスを作って、スウィフトは(C++とは違って)働く再初期化を持っているという事実のおかげで、それ自体の後にクリーンアップ:
//
// ImageAsset.swift
//
import CloudKit
import UIKit
class ImageAsset {
let image:UIImage
var url:NSURL?
var asset:CKAsset? {
get {
let data = UIImagePNGRepresentation(self.image)
self.url = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(NSUUID().UUIDString+".dat")
if let url = self.url {
do {
try data!.writeToURL(url, options: [])
} catch let e as NSError {
print("Error! \(e)")
}
return CKAsset(fileURL: url)
}
return nil
}
}
init(image:UIImage){
self.image = image
}
deinit {
if let url = self.url {
do {
try NSFileManager.defaultManager().removeItemAtURL(url) }
catch let e {
print("Error deleting temp file: \(e)")
}
}
}
}
ここ
は、それを行使するユニットテストは、(テスト対象にストップウォッチという名前のイメージがある前提)です:
//
// ImageExtensionTests.swift
//
import CloudKit
import XCTest
@testable import BudgetImpactEstimator
class ImageExtensionTests: XCTestCase {
let testImageName = "stopwatch" // provide the name of an image in test bundle
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testConvertingImageToAsset() {
guard let image = UIImage(named: self.testImageName) else {
XCTFail("failed to load image")
return
}
let imageAsset = ImageAsset(image: image)
XCTAssertNotNil(imageAsset)
guard let asset = imageAsset.asset else {
XCTFail("failed to get asset from image")
return
}
print("constructed asset: \(asset)")
}
}
はもともとUIImageの拡張機能として、それをするつもりだったが、その後deistは私がAに移動作らクラス。
出典
2016-08-13 16:53:17
Rob
読みますエラー。 'self.photoURL'はファイルURLではありません。それは何ですか? URLを印刷し、あなたの質問に出力を投稿してください。 – rmaddy
'self.photoURL'は文字列です。 Facebookのリクエストを使ってユーザーデータを取得しました。ここでは、出力されます(https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/v/t1.0-1/p200x200/555081_482184835180445_3580351_n.jpg?oh=02f1cddcf8b810f67784bb11517306f1&oe=57AFC8F3&__gda__=1474738382_fc2e585632fdb724b8dd83b273edb691) –
文字列がローカルファイルパスを表している場合は、 'string'初期化子の代わりに' fileURLWithPath'初期化子を使用して 'NSURL'を正しく作成するだけです。 – rmaddy