Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have no idea how to select a video from the camera roll using Swift in iOS. I am making an instagram app clone and I want users to only be able to select videos from their camera roll.
I have searched this question on google and only found tutorials explaining how to select images from the camera roll.
You can use this:
func getMovie() {
let selecionadorDeFoto = UIImagePickerController()
selecionadorDeFoto.delegate = self
selecionadorDeFoto.mediaTypes = [kUTTypeMovie as String]
selecionadorDeFoto.allowsEditing = false
selecionadorDeFoto.sourceType = .photoLibrary
present(selecionadorDeFoto, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// Your code here
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
This may me a simple question, but how do I detect when MPMediaPlayer is done playing in Swift 4?
You can track this using NSNotificationCenter.
func play(url: NSURL) {
let item = AVPlayerItem(URL: url)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item)
let player = AVPlayer(playerItem: item)
player.play()
}
func playerDidFinishPlaying(note: NSNotification) {
// Your code here
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm working on an iOS app(swift) that provides a faster way for users to create playlists from their Apple Music library. After reading the documentation I still can't figure out how to get acces to the users library.
Is there a way I can get accesss to all the songs in the users library and add the song ids to an array?
To access Apple's music library, you will need to add the "Privacy - Media Library Usage Description" to your info.plist. Then you need to make your class conform to MPMediaPickerControllerDelegate. To display the Apple Music library, you present the MPMediaPickerController. To add the songs to an array, you implement the didPickMediaItems method of MPMediaPickerControllerDelegate.
class MusicPicker:UIViewController, MPMediaPickerControllerDelegate {
//the songs the user will select
var selectedSongs: [URL]!
//this method is to display the music library.
func getSongs() {
var mediaPicker: MPMediaPickerController?
mediaPicker = MPMediaPickerController(mediaTypes: .music)
mediaPicker?.delegate = self
mediaPicker?.allowsPickingMultipleItems = true
mediaPicker?.showsCloudItems = false
//present the music library
present(mediaPicker!, animated: true, completion: nil)
}
//this is called when the user selects songs from the library
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
//these are the songs that were selected. We are looping over the choices
for mpMediaItem in mediaItemCollection.items {
//the song url, add it to an array
let songUrl = mpMediaItem.assetURL
selectedSongs.append(songURL)
}
//dismiss the Apple Music Library after the user has selected their songs
dismiss(animated: true, completion: nil)
}
//if the user clicks done or cancel, dismiss the Apple Music library
func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
dismiss(animated: true, completion: nil)
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have come across a few posts in different places, but do not think they fully cover what I am looking for.
By tapping a bar button item called 'Share', I would like my app to take a screenshot of the current view in the app and pop the sharing window up to share it with Twitter, Facebook and the rest of them.
You can add share button
var shareButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: "shareButtonPressed:")
self.navigationItem.rightBarButtonItem = shareButton
and populate share options
func shareButtonPressed(sender: AnyObject) {
NSLog("shareButton pressed")
let stringtoshare: String = "This is a string to share"
//add current screen shot image to share
let imagetoshare = captureScreen()
let activityItems: [AnyObject] = [stringtoshare, imagetoshare]
let activityVC: UIActivityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo]
self.presentViewController(activityVC, animated: TRUE, completion: { _ in })
}
//Capture screen shot image
func captureScreen() -> UIImage
{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image
}
Please let me know if you find any trouble in this, I am not able to test this code snip (as I am not at system) but this is how i used to handle in apps.
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 8 years ago.
Improve this question
I want to make a sound play starting from the 15th second.
How can I do it programmatically?
Thanks!
As I can understand you are trying to start your audio not from the beginning. In this case:
You need to use seekToTime
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let audioUrl = URL(string: "https://www.ringtonemobi.com/storage/upload/user_id_1/iphone-5-alarm-2016-08-21-01-49-25.mp3")!
player = AVPlayer(url: audioUrl)
player.seek(to: .init(seconds: 15, preferredTimescale: 1))
player.play()
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
in my iOS app I want to upload an image as a profile photo. So, I need something like an Icon to upload the image and then a popup or such a user interface to make a photo by camera or to upload it from the photo album on device. It should be like Facebook.
I created my app with XCode 6 and Swift. Are there any ideas, solutions or other information?
THX!
I've solved my problem with following code:
func takePhoto(sender: AnyObject) {
if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
var picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = UIImagePickerControllerSourceType.Camera
var mediaTypes: Array<AnyObject> = [kUTTypeImage]
picker.mediaTypes = mediaTypes
picker.allowsEditing = true
self.presentViewController(picker, animated: true, completion: nil)
}
else{
NSLog("No Camera.")
}
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
let selectedImage : UIImage = image
}