iOS Swift Solved Issues

532 Views

This page shows a selection of issues posted on stackoverflow.com and their relative best answers.

How to get Image src using Swift-Html-Parser

I am a newbie on Swift and Swift-HTML-Parser. I am using Swift-HTML-Parser from : https://github.com/tid-kijyun/Swift-HTML-Parser

I need some help for below question.

let myURLString = "http://MytestingWebsite.com/MyAds.html"
    let myURL = NSURL(string: myURLString)

    var error: NSError?

    let myHTMLString = NSString(contentsOfURL: myURL, encoding: NSUTF8StringEncoding, error: &error)

    if let error = error {

        //for below question 2
        println("Error : \(error)")

    } else {

        // code

    }

1) How to get the Src of the Image if there is a) only one image b) a collection of images?  

      < image Src="....."/>

2) What to use to show message when there is an error?


Adding the the required files as per the above link for Swift-HTML-Parser :

1. Add a Bridging file. click Project and goto Building Setting 
Use search: type in Bridging 
Double click on the result : Objective-C Bridging Header 
and Click (+) at the Top to add.

Error msg show: When adding file name with (-) Like Swift-HTML-Parser-Bridging-Header.h 

So, I add SwiftHTMLParserBridgingHeader

2) Copy the File Swift-HTML-Parser-Bridging-Header.h and rename it as SwiftHTMLParserBridgingHeader.h in the project file.

3) Copy HTMLParser.Swift and HTMLNode.swift

4) Have added the reference Libxml2.dylib

When compile, (2) and (3) have red dot. Am I doing something wrong?

Answer

This has a emum for each node that you can search in the HTML

public enum HTMLNodeType : String {
    case HTMLUnkownNode     = ""
    case HTMLHrefNode       = "href"
    case HTMLTextNode       = "text"
    case HTMLCodeNode       = "code"
    case HTMLSpanNode       = "span"
    case HTMLPNode          = "p"
    case HTMLLiNode         = "li"
    case HTMLUiNode         = "ui"
    case HTMLImageNode      = "image"
    case HTMLOlNode         = "ol"
    case HTMLStrongNode     = "strong"
    case HTMLPreNode        = "pre"
    case HTMLBlockQuoteNode = "blockquote"
}

From the example they have on the website:

var err : NSError?
//myHTMLString is the value you retrive from the website
var parser     = HTMLParser(html: myHTMLString, error: &err)
if err != nil {
    //This will log the error and exit the app
    //You probably should display an alert to the user
    println(err)
    exit(1)
}

var bodyNode   = parser.body

if let inputNodes = bodyNode?.findChildTags(HTMLNodeType.HTMLImageNode) {
    for node in inputNodes {
        println(node.contents)
        //this should display the address where the Image is
    }
}

Move view in direction of a specific angle

I have an angle that I am calculating based on the positioning of a view from the center of the screen. I need a way to move the view from its current position, off the screen in the direction of the angle.

I’m sure there is a fairly simple way of calculating a new x and y value, but I haven’t been able to figure out the maths. I want to do it using an animation, but I can figure that out myself once I have the coordinates.

Does anyone have any suggestions?

Answer

If you have an angle you can calculate new coordinates by getting sine and cosine values. You can try out the following code:

let pathLength = 50 as Double // total distance view should move
let piFactor = M_PI / 180
let angle = 90 as Double // direction in which you need to move it
let xCoord = outView.frame.origin.x +  CGFloat(pathLength * sin(piFactor*angle)) //outView is name of view you want to animate
let yCoord = outView.frame.origin.y +  CGFloat(pathLength * cos(piFactor*angle))
UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
    self.outView.frame = CGRectMake(xCoord, yCoord, self.outView.frame.size.width, self.outView.frame.size.height)
        }, completion: { (Bool) -> Void in
})

Source: https://stackoverflow.com/a/35245460/3724800


Pause Video Recording Using AVFoundation

I created a custom camera that works perfectly so far. I can record a video and stop recording with no errors or bugs. A feature I would like to add to this camera is the ability to pause a recording.

After a lot of research online, I see the solution is to actually stop the recording when the pause button is clicked and to begin another recording when the resume button is clicked. After that, you are supposed to merge the videos together.

I am not sure how to merge videos, I looked a lot of things up online and have not been able to find a solution.

Thank you!

This is my recording button function

@IBAction func recordVideoButtonPressed(sender:AnyObject) {
    if self.movieFileOutput.isRecording {
        isRecording = false
        self.movieFileOutput.stopRecording()
    } else {
        isRecording = true
        self.movieFileOutput.connection(withMediaType: AVMediaTypeVideo).videoOrientation = self.videoOrientation()
        self.movieFileOutput.maxRecordedDuration = self.maxRecordedDuration()
        self.movieFileOutput.startRecording(toOutputFileURL: URL(fileURLWithPath: self.videoFileLocation()), recordingDelegate: self)
    }

    self.updateRecordButtonTitle()
}

This is my pause button function

func pauseVideo() {
    if isRecording {
        if isPaused == false {
            isPaused = true
            recordButton.isEnabled = false
            recordButton.backgroundColor = UIColor.wetAsphalt
            recordButton.setTitle("Paused", for: .normal)
        } else {
            isPaused = false
            recordButton.isEnabled = true
            recordButton.backgroundColor = UIColor.red
            updateRecordButtonTitle()
        }
    } else {
        return
    }
}

Answer

You can write all the frames with AVAssetWriter. You need to use AVCaptureVideoDataOutput to get frames from the camera. Here You can find an example.

Or if you want to merge videos check out this tutorial.