I'm using the following code to add a node:
public static func Add (anchor: ARAnchor, node: SCNNode) {
guard let anchor_ = anchor as? ARWorldAnchor else { return }
let initialPos = node.presentation.position
let newNode = SCNNode()
newNode.geometry = SCNBox(width: 2 , height:1, length: 0.5, chamferRadius: 0)//
newNode.geometry?.firstMaterial?.isDoubleSided = true
newNode.geometry?.firstMaterial?.fillMode = SCNFillMode.lines
newNode.geometry?.firstMaterial?.emission.contents = UIColor.green
node.addChildNode(newNode)
}
Result:
My taget:
What do I have to do to achieve the above taget?
** 1 wish you good day!!!**
Related
I use the 'CGImageDestinationAddAuxiliaryDataInfo' try to add Auxiliary Data, this method can add a AVDepthData as AuxiliaryData to heic image.
But, this method can't help me.
my code is:
func combo(jpgfile:String, hdrGainMap: String, to heicfile:String){
let jpgImage = NSImage(contentsOfFile: jpgfile)
let url = URL(fileURLWithPath: jpgfile)
let cijpg = CIImage(contentsOf: url)
let outputURL: URL? = URL(fileURLWithPath: heicfile)
guard let cgImageDestination = CGImageDestinationCreateWithURL(outputURL! as CFURL, kUTTypeJPEG, 1, nil) else {
return
}
let context = CIContext(options: nil)
let dict: NSDictionary = [
kCGImageDestinationLossyCompressionQuality: 1.0,
kCGImagePropertyIsFloat: kCFBooleanTrue,
]
if let cgImage: CGImage = context.createCGImage(cijpg!, from: cijpg!.extent) {
CGImageDestinationAddImage(cgImageDestination, cgImage, nil)
}
var auxDataType: NSString?
var auxDic = {kCGImageAuxiliaryDataInfoData}
let gainurl = URL(fileURLWithPath: hdrGainMap)
let gainImage = CIImage(contentsOf: gainurl)
let cgGainImage = context.createCGImage(gainImage!, from: gainImage!.extent)
let bytesPerRow = cgGainImage!.bytesPerRow
let width = cgGainImage!.width
let height = cgGainImage!.height
let pixelFormat = cgGainImage?.pixelFormatInfo.rawValue
let gainData = cgGainImage?.dataProvider?.data
let auxDataDic = [kCGImageAuxiliaryDataInfoData:gainData,
kCGImageAuxiliaryDataInfoDataDescription:["BytesPerRow":bytesPerRow, "width":width, "height":height,"Orientation":1,"pixelFormat":pixelFormat]
] as [String : Any]
CGImageDestinationAddAuxiliaryDataInfo(cgImageDestination, kCGImageAuxiliaryDataTypeHDRGainMap, auxDataDic as CFDictionary)
CGImageDestinationFinalize(cgImageDestination)
}
But,the code above will cause a runtime error:
(Fig) signalled err=-17102
Is there anyway to use matrix or scale gesture to this code ? I can move image arround x,y but cant zoom in or out. Any advice would be welcome :)
this is my class
class SimpleFloatingWindow constructor(private val context: Context) {
private var windowManager: WindowManager? = null
get() {
if (field == null) field = (context.getSystemService(WINDOW_SERVICE) as WindowManager)
return field
}
private var floatView: View =
LayoutInflater.from(context).inflate(R.layout.layout_floating_window, null)
private lateinit var layoutParams: WindowManager.LayoutParams
private var lastX: Int = 0
private var lastY: Int = 0
private var firstX: Int = 0
private var firstY: Int = 0
private var isShowing = false
private var touchConsumedByMove = false
private val onTouchListener = View.OnTouchListener { view, event ->
val totalDeltaX = lastX - firstX
val totalDeltaY = lastY - firstY
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
lastX = event.rawX.toInt()
lastY = event.rawY.toInt()
firstX = lastX
firstY = lastY
}
MotionEvent.ACTION_UP -> {
view.performClick()
}
MotionEvent.ACTION_MOVE -> {
val deltaX = event.rawX.toInt() - lastX
val deltaY = event.rawY.toInt() - lastY
lastX = event.rawX.toInt()
lastY = event.rawY.toInt()
if (abs(totalDeltaX) >= 5 || abs(totalDeltaY) >= 5) {
if (event.pointerCount == 1) {
layoutParams.x += deltaX
layoutParams.y += deltaY
touchConsumedByMove = true
windowManager?.apply {
updateViewLayout(floatView, layoutParams)
}
} else {
touchConsumedByMove = false
}
} else {
touchConsumedByMove = false
}
}
else -> {
}
}
touchConsumedByMove
}
init {
with(floatView) {
closeImageButton.setOnClickListener { dismiss() }
}
floatView.setOnTouchListener(onTouchListener)
layoutParams = WindowManager.LayoutParams().apply {
format = PixelFormat.TRANSLUCENT
flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
#Suppress("DEPRECATION")
type = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ->
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
else -> WindowManager.LayoutParams.TYPE_TOAST
}
gravity = Gravity.CENTER
width = WindowManager.LayoutParams.WRAP_CONTENT
height = WindowManager.LayoutParams.WRAP_CONTENT
}
}
I'm having an issue where my search bar (embedded in a table view) doesn't show no matter what I do. It's just not there when I implement the iOS 11 navigationItem code. Search controller code is below. THANK YOU!
func configureSearchBar() {
resultSearchController = UISearchController(searchResultsController: nil)
if #available(iOS 9.0, *) {
resultSearchController.loadViewIfNeeded()
} else {
let _ = self.resultSearchController.view
}
let searchField = resultSearchController.searchBar.value(forKey: "searchField") as! UITextField
searchField.font = DifferentScreens.searchField()
resultSearchController.searchResultsUpdater = self
resultSearchController.dimsBackgroundDuringPresentation = false
resultSearchController.obscuresBackgroundDuringPresentation = false
resultSearchController.searchBar.placeholder = "Search Gifts"
if #available(iOS 11.0, *) {
navigationItem.searchController = resultSearchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
giftTableView.tableHeaderView = resultSearchController.searchBar
}
resultSearchController.searchBar.delegate = self
resultSearchController.searchBar.tintColor = UIColor.black
resultSearchController.searchBar.barTintColor = UIColor(red: 235/255, green: 235/255, blue: 235/255, alpha: 1)
definesPresentationContext = false
}
I have resigned to implementing a UISearchBar from interface builder. It seems to be working okay.
Having no luck creating a partial range in Swift 4
import Foundation
public extension String {
public var URLScheme: String? {
guard let schemeRange = self.range(of: "://") else { return nil }
return self.substring(to: schemeRange.lowerBound)
}
public var URLPortNumber: Int {
guard let portRange = self.range(of: ":", options: .backwards) else { return -1 }
let startIndex = self.index(portRange.upperBound, offsetBy: 0)
let endIndex = self.index(portRange.upperBound, offsetBy: 2)
guard self[startIndex...endIndex] != "//" else { return -1 }
return Int(self.substring(from: portRange.upperBound))!
}
public var URLHost: String {
var host = self
if let scheme = self.URLScheme {
host = host.substring(from: self.index(self.startIndex, offsetBy: (scheme + "://").characters.count))
}
if let portRange = host.range(of: ":") {
host = host.substring(to: portRange.lowerBound)
}
return host
}
}
Also after reading the documentation on Substrings, I am still less than clear on their benefit. Has anyone used them for URLs?
Even the syntax is less succinct than dot notation.
This seems to work!
import Foundation
public extension String {
public var URLScheme: String? {
guard let schemeRange = self.range(of: "://") else { return nil }
return String(describing: schemeRange.lowerBound)
}
public var URLPortNumber: Int {
guard let portRange = self.range(of: ":", options: .backwards) else { return -1 }
let startIndex = self.index(portRange.upperBound, offsetBy: 0)
let endIndex = self.index(portRange.upperBound, offsetBy: 2)
guard self[startIndex...endIndex] != "//" else { return -1 }
return Int(String(describing: portRange.upperBound))!
}
public var URLHost: String {
var host = self
if let scheme = self.URLScheme {
host = String(describing: self.index(self.startIndex, offsetBy: (scheme + "://").characters.count))
}
if let portRange = host.range(of: ":") {
host = String(describing: portRange.lowerBound)
}
return host
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do I check if my string only contains [0-9] and .?
I’d like all other symbols to be deleted from my string.
If a user inputs any String
var anyString:String = “3f00b6r.beAwes0me4me”
I want to extract all the numbers from it (including one . if present) and remove all the other characters.
var myDouble:Double = 3006.04
Someone who know how to do that ?
You can use NSRegularExpression, like this:
// First set up error for checking
var error:NSError?
// Create regular expression pattern to search for numbers one digit in length or longer
let regexString = NSRegularExpression(pattern: "[0-9]{1,}.[0-9]{1,}|[0-9]{1,}", options: nil, error: &error)
// example string
let string = "find the number 10.19"
// get the string length
let strLength = count(string)
// To return the result first of all obtain the range of the first match if there is one, if let used here because NSRegularExpression returns an optional (we could of checked earlier, but I didn't)
if let rangeOfFirstMatch = regexString?.rangeOfFirstMatchInString(string, options: nil, range: NSMakeRange(0, strLength))
{
// if a range has been found that isn't equal to NSNotFound then build a range for the found string
if !NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)) {
advance(string.startIndex, rangeOfFirstMatch.length)
let r = Range(start: advance(string.startIndex, rangeOfFirstMatch.location), end: advance(string.startIndex, rangeOfFirstMatch.location+rangeOfFirstMatch.length))
// return the value
let substringForFirstMatch = string.substringWithRange(r)
println("Here's your match: \(substringForFirstMatch)")
}
}
There's also String.stringByTrimmingCharactersInSet() which would be cleaner and quicker but isn't as versatile. For instance the above string would also be returned if you did this:
let set = NSMutableCharacterSet.alphanumericCharacterSet()
set.addCharactersInString(" ")
set.removeCharactersInString("0123456789.")
string.stringByTrimmingCharactersInSet(set)
But if the string changed to "find the number ??...10.19...." then the stringByTrimmingCharactersInSet() would let you down and return "??...10.19...." while the regular expression version would hold up.
To make things easier a String extension could be written like this for returning multiple numbers in a string of type Double or Int:
extension String {
func stringsMatchingRegularExpression(expression exp:String, error err:NSErrorPointer) -> [String]? {
var strArray:[String]?
var rangeArray:[NSRange]?
let strLength = count(self)
var startOfRange = 0
if let regexString = NSRegularExpression(pattern: exp, options: nil, error: err) {
while startOfRange <= strLength {
let rangeOfMatch = regexString.rangeOfFirstMatchInString(self, options: nil, range: NSMakeRange(startOfRange, strLength-startOfRange))
if let rArray = rangeArray {
rangeArray = rArray + [rangeOfMatch]
}
else {
rangeArray = [rangeOfMatch]
}
startOfRange = rangeOfMatch.location+rangeOfMatch.length
}
if let ranArr = rangeArray {
for r in ranArr {
if !NSEqualRanges(r, NSMakeRange(NSNotFound, 0)) {
advance(self.startIndex, r.length)
let r = Range(start: advance(self.startIndex, r.location), end: advance(string.startIndex, r.location+r.length))
// return the value
let substringForMatch = self.substringWithRange(r)
if let sArray = strArray {
strArray = sArray + [substringForMatch]
}
else {
strArray = [substringForMatch]
}
}
}
}
}
return strArray
}
}
let myString = "one number is 7.5, another is 20"
let subStringArray = myString.stringsMatchingRegularExpression(expression: "[0-9]{1,}.[0-9]{1,}|[0-9]{1,}", error: nil)
subStringArray?[0] // 7.5
subStringArray?[1] // 20
Swift 2.0 Update for Extension
extension String {
func stringsMatchingRegularExpression(expression exp:String) -> [String]? {
var strArray:[String]?
var rangeArray:[NSRange]?
let strLength = self.characters.count
var startOfRange = 0
do {
let regexString = try NSRegularExpression(pattern: exp, options: [])
while startOfRange <= strLength {
let rangeOfMatch = regexString.rangeOfFirstMatchInString(self, options: [], range: NSMakeRange(startOfRange, strLength-startOfRange))
if let rArray = rangeArray {
rangeArray = rArray + [rangeOfMatch]
}
else {
rangeArray = [rangeOfMatch]
}
startOfRange = rangeOfMatch.location+rangeOfMatch.length
}
if let ranArr = rangeArray {
for r in ranArr {
if !NSEqualRanges(r, NSMakeRange(NSNotFound, 0)) {
self.startIndex.advancedBy(r.length)
let r = Range(start: self.startIndex.advancedBy(r.location), end: self.startIndex.advancedBy(r.location + r.length))
// return the value
let substringForMatch = self.substringWithRange(r)
if let sArray = strArray {
strArray = sArray + [substringForMatch]
}
else {
strArray = [substringForMatch]
}
}
}
}
} catch {
}
return strArray
}
}
let myString = "one number is 7.5, another is 20"
let subStringArray = myString.stringsMatchingRegularExpression(expression: "[-+]?\\d+.?\\d+")
subStringArray?[0] // 7.5
subStringArray?[1] // 20
Swift 3.0 Update for Extension
extension String {
func stringsMatchingRegularExpression(expression exp:String) -> [String]? {
var strArray:[String]?
var rangeArray:[NSRange]?
let strLength = self.characters.count
var startOfRange = 0
do {
let regexString = try NSRegularExpression(pattern: exp, options: [])
while startOfRange <= strLength {
let rangeOfMatch = regexString.rangeOfFirstMatch(in: self, options: [], range: NSMakeRange(startOfRange, strLength-startOfRange))
if let rArray = rangeArray {
rangeArray = rArray + [rangeOfMatch]
}
else {
rangeArray = [rangeOfMatch]
}
startOfRange = rangeOfMatch.location+rangeOfMatch.length
}
if let ranArr = rangeArray {
for r in ranArr {
if !NSEqualRanges(r, NSMakeRange(NSNotFound, 0)) {
self.index(startIndex, offsetBy: r.length)
let r = self.index(startIndex, offsetBy:r.location)..<self.index(startIndex, offsetBy:r.location + r.length)
// return the value
let substringForMatch = self.substring(with: r)
if let sArray = strArray {
strArray = sArray + [substringForMatch]
}
else {
strArray = [substringForMatch]
}
}
}
}
}
catch {
// catch errors here
}
return strArray
}
}
let myString = "one number is 7.5, another is 20"
let subStringArray = myString.stringsMatchingRegularExpression(expression: "[-+]?\\d+.?\\d+")
subStringArray?[0] // 7.5
subStringArray?[1] // 20