Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
  2. {
  3. picker.dismissViewControllerAnimated(true, completion: nil)
  4.  
  5. let pickedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
  6.  
  7. if picker.sourceType == .Camera
  8. {
  9. // When taking a picture with the camera, store it in the user roll
  10. PHPhotoLibrary.sharedPhotoLibrary().performChanges(
  11. { () -> Void in
  12.  
  13. // save the image
  14. PHAssetCreationRequest.creationRequestForAssetFromImage(pickedImage)
  15.  
  16. // TODO how to get the asset url
  17.  
  18. }, completionHandler:
  19. { (finished, error) -> Void in
  20. if (finished)
  21. {
  22.  
  23. }
  24. }
  25. )
  26.  
  27. }
  28. else
  29. {
  30. let pickedImageUrl: NSURL? = info[UIImagePickerControllerReferenceURL] as? NSURL
  31.  
  32. currentImageUrl = pickedImageUrl
  33.  
  34. currentImage = pickedImage
  35.  
  36. toggleImageInfoView(true)
  37. toggleMapState(true)
  38. }
  39.  
  40.  
  41. }
  42.  
  43. UIImageWriteToSavedPhotosAlbum(image.image, self, #selector(cameraImageSavedAsynchronously), nil)
  44.  
  45. func fetchLastImage(completion: (localIdentifier: String?) -> Void)
  46. {
  47. let fetchOptions = PHFetchOptions()
  48. fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
  49. fetchOptions.fetchLimit = 1
  50.  
  51. let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
  52. if (fetchResult.firstObject != nil)
  53. {
  54. let lastImageAsset: PHAsset = fetchResult.firstObject as! PHAsset
  55. completion(localIdentifier: lastImageAsset.localIdentifier)
  56. }
  57. else
  58. {
  59. completion(localIdentifier: nil)
  60. }
  61. }
  62.  
  63. let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
  64. fetchResult.firstObject
  65.  
  66. __block PHObjectPlaceholder *placeholderAsset = nil;
  67. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  68. PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
  69. newAssetRequest.location = location;
  70. newAssetRequest.creationDate = [NSDate date];
  71. placeholderAsset = newAssetRequest.placeholderForCreatedAsset;
  72. } completionHandler:^(BOOL success, NSError *error) {
  73. if(success){
  74. PHAsset *asset = [self getAssetFromlocalIdentifier:placeholderAsset.localIdentifier];
  75. completionBlock(asset, YES);
  76. } else {
  77. completionBlock(nil, NO);
  78. }
  79. }];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement