Friday, December 21, 2018

Upload UIImage as base64 String

Upload UIImage as Base64 String

(Upload UIImage as string)

//Select Pic From Camera or Gallery 
    

@objc func btnProfileImageSelection(geture:UIGestureRecognizer) {
        //Create the AlertController and add Its action like button in Actionsheet
        let actionSheetControllerIOS8: UIAlertController = UIAlertController(title: "Choose photo...", message: "", preferredStyle: .actionSheet)
        
        let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) { _ in
            print("Cancel")
        }
        actionSheetControllerIOS8.addAction(cancelActionButton)
        
        let saveActionButton = UIAlertAction(title: "Camera", style: .default)
        { _ in
            print("Camera")
            
            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
                let imag = UIImagePickerController()
                imag.delegate = self
                imag.sourceType = UIImagePickerController.SourceType.camera;
                imag.allowsEditing = true
                self.present(imag, animated: true, completion: nil)
            }
            
        }
        actionSheetControllerIOS8.addAction(saveActionButton)
        
        let deleteActionButton = UIAlertAction(title: "Gallery", style: .default)
        { _ in
            print("Gallery")
            
            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
                let imag = UIImagePickerController()
                imag.delegate = self
                imag.sourceType = UIImagePickerController.SourceType.photoLibrary;
                //imag.mediaTypes = [kUTTypeImage];
                imag.allowsEditing = true
                self.present(imag, animated: true, completion: nil)
                
            }
        }
        actionSheetControllerIOS8.addAction(deleteActionButton)
        self.present(actionSheetControllerIOS8, animated: true, completion: nil)
        
    }
    
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        // The info dictionary may contain multiple representations of the image. You want to use the original.
        guard let selectedImage = info[.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }
        
        // Set photoImageView to display the selected image.
        self.imgUser.image = selectedImage
//Compress image quality
        let imageData = selectedImage.jpegData(compressionQuality: 0.1)
        
//Here Image converted into base64 string

                let strBase64 = imageData?.base64EncodedString() ?? ""         picker.dismiss(animated: true, completion: nil)

    }

Thursday, July 5, 2018

Set UIView Shadow

Set UIView Shadow


//MARK: View Shadow
    func viewShadow(myView: UIView) -> UIView {
        myView.layer.shadowColor = UIColor.black.cgColor
        myView.layer.shadowOpacity = 1
        myView.layer.shadowOffset = CGSize.zero
        myView.layer.shadowRadius = 1
        myView.layer.shadowPath = UIBezierPath(rect: myView.bounds).cgPath
        myView.layer.shouldRasterize = true
        return myView
    }

Time calculation from given date

This will show no of seconds, minutes, hours, days, weeks, months and year passed or yet to pass from given date.



public func timeAgoSince(_ date: Date) -> String {
    
    let calendar = Calendar.current
    let now = Date()
    let unitFlags: NSCalendar.Unit = [.second, .minute, .hour, .day, .month, .year]
    let components = (calendar as NSCalendar).components(unitFlags, from: date, to: now, options: [])
    
    if let year = components.year, year >= 2 {
        return "\(year) years ago"
    }
    
    if let year = components.year, year >= 1 {
        return "Last year"
    }
    
    if let month = components.month, month >= 2 {
        return "\(month) months ago"
    }
    
    if let month = components.month, month >= 1 {
      return "\(month) month ago"
        // return "Last month"
    }
    
//    if let week = components.weekOfYear, week >= 2 {
//        return "\(week) weeks ago"
//    }
//    
//    if let week = components.weekOfYear, week >= 1 {
//        return "Last week"
//    }
    
    if let day = components.day, day >= 2 {
        return "\(day) days ago"
    }
    
    if let day = components.day, day >= 1 {
        return "\(day) day ago"
        //return "Yesterday"
    }
    
    if let hour = components.hour, hour >= 2 {
        return "\(hour) hours ago"
    }
    
    if let hour = components.hour, hour >= 1 {
        return "An hour ago"
    }
    
    if let minute = components.minute, minute >= 2 {
        return "\(minute) minutes ago"
    }
    
    if let minute = components.minute, minute >= 1 {
        return "A minute ago"
    }
    
    if let second = components.second, second >= 3 {
        return "\(second) seconds ago"
    }
    
    if let year = components.year, year < 0 {
        if(abs(year) == 1){
            return "\(abs(year)) year to go"}
        else{
            return "\(abs(year)) years to go"}
    }
    
    if let month = components.month, month < 0 {
        if(abs(month) == 1){
            return "\(abs(month)) month to go"}
        else{
            return "\(abs(month)) months to go"}
    }
    
//    if let week = components.weekOfYear, week < 0 {
//        if(abs(week) == 1){
//            return "\(abs(week)) week to go"}
//        else{
//            return "\(abs(week)) weeks to go"}
/   }
    
    if let day = components.day, day < 0 {
        if(abs(day) == 1){
            return "\(abs(day)) day to go"}
        else{
            return "\(abs(day)) days to go"}
    }
    
    if let hour = components.hour, hour < 0 {
        if(abs(hour) == 1){
            return "\(abs(hour)) hour to go"}
        else{
            return "\(abs(hour)) hours to go"}
    }
    
    
    if let minute = components.minute, minute < 0 {
        if(abs(minute) == 1){
            return "\(abs(minute)) minute to go"}
        else{
            return "\(abs(minute)) minutes to go"}
    }
    if let sec = components.second, sec < 0 {
        if(abs(sec) == 1){
            return "\(abs(sec)) second to go"}
        else{
            return "\(abs(sec)) seconds to go"}
    }
    
    return "Just now"    
}

Upload UIImage as base64 String

Upload UIImage as Base64 String (Upload UIImage as string) //Select Pic From Camera or Gallery       @objc func btnPro...