title: Go from image url rep from asset library to base64
body: <p>I am ultimately trying to convert an array of photos that are in a dictionary in url rep form to base64 to be sent over json. </p>
  
  <p>Here is the dictionary code and log of it:</p>
  
  <pre><code>    NSDictionary *dict = [self.form dictionaryWithValuesForKeys:keys];
      NSLog(@"dict::%@",dict);
  </code></pre>
  
  <p>NSLog:</p>
  
  <pre><code>dict::{   
      boardLodgingFurnished = "&lt;null&gt;";
      caption = "&lt;null&gt;";
      cars = "";
      photos =     (
                  {
              caption = "";
              urlRep = "assets-library://asset/asset.JPG?id=CE8A426B-3B59-4172-8761-CC477F3BB3EE&amp;ext=JPG";
          },
                  {
              caption = "";
              urlRep = "assets-library://asset/asset.JPG?id=F4B68A42-1CA0-4880-9FB5-177CB091A28C&amp;ext=JPG";
          }
      );
      yearsAtLocation = "";
      yearsInTheBusiness = "";
  }
  </code></pre>
  
  <p>So for each photo in the dictionary I would like to take the urlRep and convert that to a base64 string and replace the urlRep with it in the dictionary.</p>
  
  <p>What I have right now..not sure if I am going in the right direction:</p>
  
  <pre><code>for (id imageURL in [dict objectForKey:@"photos"])
  {
  ALAssetsLibrary *library = [ALAssetsLibrary new];
              ALAsset *ourAsset = [self assetForURL:imageURL withLibrary:library];
  
              /* Check out ALAssets */
              NSLog(@"%@", ourAsset);
  
              ALAssetRepresentation *representation = [ourAsset defaultRepresentation];
              CGImageRef imageRef = [representation fullResolutionImage];
  
              //TODO: Deal with JPG or PNG
              NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 0.1);
         NSLog(@"imagedata??%@",      [imageData base64EncodedString]);
  //need to know how to add this back to dict
  }
  </code></pre>
  
  <p>the below method is called from above but crashes on the while loop with </p>
  
  <pre><code>-[__NSDictionaryI scheme]: unrecognized selector sent to instance 0x166dd090
  2014-01-03 10:57:27.361 Inspection App[2728:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI scheme]: unrecognized selector sent to instance 0x166dd090'
  </code></pre>
  
  <p>method</p>
  
  <pre><code>- (ALAsset *)assetForURL:(NSURL *)url withLibrary:(ALAssetsLibrary *)assetsLibrary {
      __block ALAsset *result = nil;
      __block NSError *assetError = nil;
      dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  
      [assetsLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
          result = asset;
          dispatch_semaphore_signal(sema);
      } failureBlock:^(NSError *error) {
          assetError = error;
          dispatch_semaphore_signal(sema);
      }];
  
  
      if ([NSThread isMainThread]) {
          while (!result &amp;&amp; !assetError) {
              [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
          }
      }
      else {
          dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
      }
  
      return result;
  }
  </code></pre>
  
  <p><strong>edit:</strong></p>
  
  <pre><code>if (photoUrls.count) {
              for (id photos in photoUrls){
              NSString *urlString = photos;
              [self base64ImageAtUrlString:urlString result:^(NSString *base64) {
  
                  NSLog(@"imagedata??%@", base64);
              }];
          }
          }
          else {
              NSLog(@"where are my urls?");
          }
  
          NSMutableDictionary *jsonWithPhotos = [dict mutableCopy];
  
          [jsonWithPhotos setObject:convertedImages forKey:@"photo64"];
          NSLog(@"jjson photos::%@", jsonWithPhotos);
  </code></pre>
  
  <p>updated method </p>
  
  <pre><code>- (void)base64ImageAtUrlString:(NSString *)urlString result:(void (^)(NSString *))completion {
      NSURL *url = [NSURL URLWithString:urlString];
      ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  
      [library assetForURL:url resultBlock:^(ALAsset *asset) {
  
          // borrowing your code, here... didn't check it....
          ALAssetRepresentation *representation = [asset defaultRepresentation];
          CGImageRef imageRef = [representation fullResolutionImage];
  
          //TODO: Deal with JPG or PNG
          NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 0.1);
          NSString *base64 = [imageData base64EncodedString];
          completion(base64);
          [convertedImages addObject:base64];
  
  //        NSLog(@"converted::%@",convertedImages);
  
      } failureBlock:^(NSError *error) {
          NSLog(@"that didn't work %@", error);
      }];
  }
  </code></pre>
  
  <p>When i log jsonWithPhotos the object photo64 is just a blank array</p>