从照片库iOS获取ALAssets

时间:2016-10-10 17:56:22

标签: objective-c xcode ios7 alassetslibrary alasset

我想从我在iPhone / iPad上拍摄的电影中获取时间,日期和地点这是我使用的代码

//我选择了视频,然后将其保存到应用中,但我想在保存之前我正在获取电影的资产数据。

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{   NSError *error;
 else if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {

    NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:movieURL
                   resultBlock:^(ALAsset*asset) {
                       NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate];
                       NSLog(@"Date: %@", myDate);

                   } failureBlock:^(NSError *error) {
                       NSLog(@"Error");
                   }];
    NSData * movieData = [NSData dataWithContentsOfURL:movieURL];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *slash = [documentsDirectory stringByAppendingPathComponent:@"/"];
    NSString *documentsDirectory2 = [slash stringByAppendingPathComponent:self.user.text];
    NSString *documentsDirectory21 = [documentsDirectory2 stringByAppendingPathComponent:@"/Movie"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory21])
        [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory21 withIntermediateDirectories:NO attributes:nil error:&error];
    NSString *fullPath = [documentsDirectory21 stringByAppendingPathComponent:[[self imageNameTextField]text]];
    fullPath = [fullPath stringByAppendingFormat:@".mp4"];
    [ movieData writeToFile:fullPath atomically:YES];
} 

但myDate记录为null

我做错了什么

我刚试过这段代码(已添加10/11)

CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)movieURL,NULL);

    NSDictionary* metadata = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
    NSLog(@"image data %@", metadata);

但仍然元数据为空;

我读到视频不具有与图像相同的元数据。如果这是真的,那么我如何访问视频元数据,因为我可以看到与照片中的视频相关的数据。

任何帮助将不胜感激。

我已经尝试过我能在互联网上找到的所有东西来帮助我做到这一点,但似乎没有任何效果。 当我记录我的movieURL时,这就是我得到的

2016-10-13 09:58:51.045271 my little world app[2641:1388034]     movieURL;file:///private/var/mobile/Containers/Data/Application/22A178E5-74C4-    48EF-B487-5E01321508AD/tmp/trim.04F0AC8A-2960-464D-8670-7E79662EAB9B.MOV

因此,由于它位于临时文件中,因此我无法获取元数据。

元数据是否会在电影NSData中退出,我可以从中获取吗?

我正在拔头发。

1 个答案:

答案 0 :(得分:0)

所以我在这里想到的是代码

     case class Data(totalValue: Long, typeCount: Map[String,Int], typeForDay: Map[String,Int] ,itemCount : Map[String,Int]) extends Serializable

         def convertToRDD(result : DataFrame): RDD[(String, String)] = {

            val tempFile = result.map( {
              r => {
                val customerId = r.getAs[String]( "CUSTOMER_ID" )
                val totalValue = r.getAs[Long]( "totalValue" )

                val typeCount = r.getAs[Map[String, Int]]( "typeCount" )
                val itemCount = r.getAs[Map[String, Int]]( "itemCount" )
                val typeForDay = r.getAs[Map[String, Int]]( "typeForDay" )


                val features = Data( totalValue, typeCount, typeForDay, itemCount)

                val jsonString = JacksonUtil.toJson( features )

                (customerId, jsonString)
              }
            } )

            return tempFile
          }

    class CollectListFunction[T] (val colType: DataType) extends UserDefinedAggregateFunction {

      def inputSchema: StructType =
        new StructType().add("inputCol", colType)

      def bufferSchema: StructType =
        new StructType().add("outputCol", ArrayType(colType))

      def dataType: DataType = ArrayType(colType)

      def deterministic: Boolean = true

      def initialize(buffer: MutableAggregationBuffer): Unit = {
        buffer.update(0, new scala.collection.mutable.ArrayBuffer[T])
      }

      def update(buffer: MutableAggregationBuffer, input: Row): Unit = {
        val list = buffer.getSeq[T](0)
        if (!input.isNullAt(0)) {
          val sales = input.getAs[T](0)
          buffer.update(0, list:+sales)
        }
      }

      def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = {
        buffer1.update(0, buffer1.getSeq[T](0) ++ buffer2.getSeq[T](0))
      }

      def evaluate(buffer: Row): Any = {
        buffer.getSeq[T](0)
      }
    }



    object CombineMaps extends UserDefinedAggregateFunction {
      override def inputSchema: StructType = new StructType().add("map", dataType)
      override def bufferSchema: StructType = inputSchema
      override def dataType: DataType = MapType(StringType, IntegerType)
      override def deterministic: Boolean = true

      override def initialize(buffer: MutableAggregationBuffer): Unit = buffer.update(0 , Map[String, Int]())

      override def update(buffer: MutableAggregationBuffer, input: Row): Unit = {
        val before = buffer.getAs[Map[String, Int]](0)
        val toAdd = input.getAs[Map[String, Int]](0)
        val result = before ++ toAdd
        buffer.update(0, result)
      }

      override def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = update(buffer1, buffer2)

      override def evaluate(buffer: Row): Any = buffer.getAs[Map[String, Int]](0)
    }

您可以使用任何valueForProperty