Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift如何在手表上显示文本存储_Ios_Swift_Cocoa Touch_Watchkit_Apple Watch - Fatal编程技术网

Ios Swift如何在手表上显示文本存储

Ios Swift如何在手表上显示文本存储,ios,swift,cocoa-touch,watchkit,apple-watch,Ios,Swift,Cocoa Touch,Watchkit,Apple Watch,我有一个文本视图与图像,不同的文本样式等,我想显示在手表上。有什么办法吗 它不是在apple watch上显示NSAttributed String的代码,但我使用它发送NSAttributed String,并以更易于显示的形式显示图像。 代码可能不是高质量的(我几乎肯定不是,尤其是缩放图像) func sendText(stext:nsmutableAttributeString){ dispatch\u async(dispatch\u get\u main\u queue()){ let

我有一个文本视图与图像,不同的文本样式等,我想显示在手表上。有什么办法吗

它不是在apple watch上显示NSAttributed String的代码,但我使用它发送NSAttributed String,并以更易于显示的形式显示图像。 代码可能不是高质量的(我几乎肯定不是,尤其是缩放图像)

func sendText(stext:nsmutableAttributeString){
dispatch\u async(dispatch\u get\u main\u queue()){
let range:NSRange=NSMakeRange(0,stext.length)
stext.enumerateAttributesInRange(范围,选项:NSAttributedStringEnumerationOptions.init(rawValue:0),使用块:{(dic:[String:AnyObject]?,范围:NSRange!,停止:UnsafeMutablePointer)->中的Void
self.attachement=dic作为NSDictionary!
self.attachement.enumerateKeysAndObjectsUsingBlock({(key:AnyObject?,obj:AnyObject?,stop:UnsafeMutablePointer)->中的Void
让stri=键为!NSString!
如果stri==“NSAttachment”{
设att:NSTextAttachment=obj为!NSTextAttachment
如果att.image==nil{
打印(“无图像”)
}否则{
var im:UIImage=att.image!
打印(“图像存在”)
如果im.size.height>340和im.size.width>272{
let heightScale:CGFloat=self.textView.frame.size.height/im.size.height
让宽度缩放:CGFloat=self.textView.frame.size.width/im.size.width
如果高度刻度<宽度刻度{
让高度=im.size.height*高度刻度*0.7
宽度=im.size.width*高度刻度*0.7
UIGraphicsBeginImageContext(CGSizeMake(宽度、高度))
im.drawInRect(CGRectMake(0,0,宽度,高度))
im=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
}否则{
让高度=im.size.height*宽度比例*0.7
让宽度=im.size.width*widthScale*0.7
UIGraphicsBeginImageContext(CGSizeMake(宽度、高度))
im.drawInRect(CGRectMake(0,0,宽度,高度))
im=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
}
}
self.lastRange=rang.location
self.finalForWatch.append(NSAttributedString(attributedString:stext.attributedSubstringFromRange(NSMakeRange(0,rang.location)))
self.finalForWatch.append(im)
}
}
如果self.lastRange==0{
self.finalForWatch.append(stext)
}否则{
self.finalForWatch.append(NSAttributedString(attributedString:stext.attributedSubstringFromRange(NSMakeRange(1,stext.length-1)))
}
})
})
let data:NSData=NSKeyedArchiver.archivedDataWithRootObject(self.finalForWatch)
让ApplicationICT=[“完成”:数据]
让documentDirectoryURL=try!NSFileManager.defaultManager().UrlfDirectory(.DocumentDirectory,inDomain:.UserDomainMask,ApproveForURL:nil,create:true)
让fileDestinationUrl=documentDirectoryURL.URLByAppendingPathComponent(“file.txt”)
做{
try data.writeToURL(fileDestinationUrl,原子性:true)
}将let错误捕获为NSError{
打印(“写入url\(fileDestinationUrl)时出错”)
打印(错误。本地化描述)
}
self.session?.transferFile(fileDestinationUrl,元数据:nil)
}
}

我将所有内容保存到一个文件中,并使用sendFile方法,因为applicationcontext、userInfo和消息总是返回“太大的有效载荷”。

这不是在apple watch上显示NSAttributedString的代码,但我使用它发送NSAttributedString,并以更易于显示的形式显示图像。 代码可能不是高质量的(我几乎肯定不是,尤其是缩放图像)

func sendText(stext:nsmutableAttributeString){
dispatch\u async(dispatch\u get\u main\u queue()){
let range:NSRange=NSMakeRange(0,stext.length)
stext.enumerateAttributesInRange(范围,选项:NSAttributedStringEnumerationOptions.init(rawValue:0),使用块:{(dic:[String:AnyObject]?,范围:NSRange!,停止:UnsafeMutablePointer)->中的Void
self.attachement=dic作为NSDictionary!
self.attachement.enumerateKeysAndObjectsUsingBlock({(key:AnyObject?,obj:AnyObject?,stop:UnsafeMutablePointer)->中的Void
让stri=键为!NSString!
如果stri==“NSAttachment”{
设att:NSTextAttachment=obj为!NSTextAttachment
如果att.image==nil{
打印(“无图像”)
}否则{
var im:UIImage=att.image!
打印(“图像存在”)
如果im.size.height>340和im.size.width>272{
let heightScale:CGFloat=self.textView.frame.size.height/im.size.height
让宽度缩放:CGFloat=self.textView.frame.size.width/im.size.width
如果高度刻度<宽度刻度{
让高度=im.size.height*高度刻度*0.7
宽度=im.size.width*高度刻度*0.7
UIGraphicsBeginImageContext(CGSizeMake(宽度、高度))
im.drawInRect(CGRectMake(0,0,宽度,高度))
im=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
}否则{
L
    func sendText(stext: NSMutableAttributedString) {
    dispatch_async(dispatch_get_main_queue()) {
    let range:NSRange = NSMakeRange(0, stext.length)
    stext.enumerateAttributesInRange(range, options: NSAttributedStringEnumerationOptions.init(rawValue: 0), usingBlock: { (dic: [String:AnyObject]?, rang:NSRange!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in


        self.attachement = dic as NSDictionary!
        self.attachement.enumerateKeysAndObjectsUsingBlock({ (key:AnyObject?, obj:AnyObject?, stop:UnsafeMutablePointer<ObjCBool>) -> Void in


            let stri = key as! NSString!
            if stri == "NSAttachment"{
                let att:NSTextAttachment = obj as! NSTextAttachment
                if att.image == nil{
                    print("no image")
                }else{
                    var im:UIImage = att.image!

                    print("image exists")

                    if im.size.height > 340 && im.size.width > 272 {
                        let heightScale: CGFloat = self.textView.frame.size.height / im.size.height
                        let widthScale: CGFloat = self.textView.frame.size.width / im.size.width

                        if heightScale < widthScale {
                            let height = im.size.height * heightScale * 0.7
                            let width = im.size.width * heightScale * 0.7
                            UIGraphicsBeginImageContext(CGSizeMake(width, height))
                            im.drawInRect(CGRectMake(0, 0, width, height))
                            im = UIGraphicsGetImageFromCurrentImageContext()
                            UIGraphicsEndImageContext()
                        } else {
                            let height = im.size.height * widthScale * 0.7
                            let width = im.size.width * widthScale * 0.7
                            UIGraphicsBeginImageContext(CGSizeMake(width, height))
                            im.drawInRect(CGRectMake(0, 0, width, height))
                            im = UIGraphicsGetImageFromCurrentImageContext()
                            UIGraphicsEndImageContext()
                        }

                    }
                    self.lastRange = rang.location
                    self.finalForWatch.append(NSAttributedString(attributedString: stext.attributedSubstringFromRange(NSMakeRange(0, rang.location))))
                    self.finalForWatch.append(im)

                }
            }
            if self.lastRange == 0 {
                self.finalForWatch.append(stext)
            } else {
                self.finalForWatch.append(NSAttributedString(attributedString: stext.attributedSubstringFromRange(NSMakeRange(1, stext.length-1))))
            }
        })

    })
        let data: NSData = NSKeyedArchiver.archivedDataWithRootObject(self.finalForWatch)
        let applicationDict = ["done" : data]

        let documentDirectoryURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)

        let fileDestinationUrl = documentDirectoryURL.URLByAppendingPathComponent("file.txt")

        do{
            try data.writeToURL(fileDestinationUrl, atomically: true)
        } catch let error as NSError {
            print("error writing to url \(fileDestinationUrl)")
            print(error.localizedDescription)
        }

          self.session?.transferFile(fileDestinationUrl, metadata: nil)

    }
}