2016-03-04 61 views
0

CGImageSourceCreateWithURL return always nil。我正在传递[NSURL fileURLWithPath:getImagePath]。我仔细检查了图像是否存在于我正在传递的网址中。请找出下面的代码。CGImageSourceCreateWithURL return always nil

NSURL *imageURL = [self getImageAtTime:i withTitle:@"gifImage"]; 

if (!imageURL) { 
    NSLog(@"imageURL is null"); 
    return; 
} 

CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)imageURL, NULL); 

if (source == NULL) { 
    NSLog(@"Source is NULL"); 
} 

CGImageDestinationAddImageFromSource(destination, source, i+1, (__bridge CFDictionaryRef)frameProperties); 
+0

我的项目也是ARC启用。这里是图像文件路径文件:///var/mobile/Containers/Data/Application/A54E8DBF-70AE-4C4A-BD57-9A2D26545C87/Documents/gifImage1.png –

+0

请点击这里:http://stackoverflow.com/questions/21477186/cgimagesourcecreatewithurl-returns-always-null – gvuksic

+0

我已经检查了这个链接。图像保存在cacheDirectory上。所以我使用fileURLWithPath:getImagePath。但没有找到任何解决方案。 –

回答

0

同时创造CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)imageURL, NULL);我只是忘了指定图像的属性。以下是CGImageSourceCreateWithURL的工作代码

  NSURL *imageURL = [self getImageAtTime:i withTitle:@"gifImage"]; 

      if (!imageURL) { 
       NSLog(@"imageURL is null"); 
       return; 
      } 

      CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)imageURL, NULL); 

      if (source == NULL) { 
       NSLog(@"Source is NULL"); 
      } 

      //get all the metadata in the image 
      NSDictionary *metadata = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source, 0, NULL); 

      //make the metadata dictionary mutable so we can add properties to it 
      NSMutableDictionary *metadataAsMutable = [metadata mutableCopy]; 

      NSMutableDictionary *EXIFDictionary = [[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary]mutableCopy]; 
      NSMutableDictionary *GPSDictionary = [[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGPSDictionary]mutableCopy]; 
      NSMutableDictionary *RAWDictionary = [[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyRawDictionary]mutableCopy]; 
      NSMutableDictionary *GIFDictionary = [[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGIFDictionary]mutableCopy]; 

      if(!EXIFDictionary) 
       EXIFDictionary = [[NSMutableDictionary dictionary] init]; 

      if(!GPSDictionary) 
       GPSDictionary = [[NSMutableDictionary dictionary] init]; 

      if(!RAWDictionary) 
       RAWDictionary = [[NSMutableDictionary dictionary] init]; 

      if(!GIFDictionary) 
       GIFDictionary = [[NSMutableDictionary dictionary] init]; 


      [GPSDictionary setObject:@"camera coord Latitude" 
           forKey:(NSString*)kCGImagePropertyGPSLatitude]; 
      [GPSDictionary setObject:@"camera coord Longitude" 
           forKey:(NSString*)kCGImagePropertyGPSLongitude]; 
      [GPSDictionary setObject:@"camera GPS Date Stamp" 
           forKey:(NSString*)kCGImagePropertyGPSDateStamp]; 
      [GPSDictionary setObject:@"camera direction (heading) in degrees" 
           forKey:(NSString*)kCGImagePropertyGPSImgDirection]; 

      [GPSDictionary setObject:@"subject coord Latitude" 
           forKey:(NSString*)kCGImagePropertyGPSDestLatitude]; 
      [GPSDictionary setObject:@"subject coord Longitude" 
           forKey:(NSString*)kCGImagePropertyGPSDestLongitude]; 

      [EXIFDictionary setObject:@"[S.D.] kCGImagePropertyExifUserComment" 
           forKey:(NSString *)kCGImagePropertyExifUserComment]; 

      [EXIFDictionary setValue:@"69 m" forKey:(NSString *)kCGImagePropertyExifSubjectDistance]; 

      [GIFDictionary setObject:[NSNumber numberWithFloat:duration/frameCount] forKey:(NSString *)kCGImagePropertyGIFDelayTime]; 


      //Add the modified Data back into the image’s metadata 
      [metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary]; 
      [metadataAsMutable setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary]; 
      [metadataAsMutable setObject:RAWDictionary forKey:(NSString *)kCGImagePropertyRawDictionary]; 
      [metadataAsMutable setObject:GIFDictionary forKey:(NSString *)kCGImagePropertyGIFDictionary]; 

      CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metadataAsMutable); 

      //   CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); 

      CFRelease(source);