Objective-C/CocoaHTTPServer - Return an image how response

1.2k views Asked by At

I have this simple method that check if i pass a parameter like "/testPhoto", and if, it is passed, i want try to answer with a simple image that has the path that you can see in the variable "testPath" (static path for try). At the moment, when i do the request, i receive the 200 ok status from the server, but no data is passed (0 Bytes). I need to understand what i'm doing wrong. Maybe the testPath doesn't contains a correct path? The path that i'm using is found using the ALAssetslibrary.

- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path{

  HTTPLogTrace();

  if ([path isEqualToString:@"/testPhoto"]){
    NSString *testPath = [[NSString alloc] init];
    testPath = @"assets-library://asset/asset.JPG?id=DB96E240-8760-4FD6-B8B4-FEF3F61793B3&ext=JPG";
    NSURL *deviceImageUrl = [[NSURL alloc] initWithString:testPath];
    NSData *imageData = [NSData dataWithContentsOfURL:deviceImageUrl];
    UIImage *deviceImage = [UIImage imageWithData:imageData];

    HTTPDataResponse *photoResponse = [[HTTPDataResponse alloc] initWithData:imageData];
    return photoResponse;
  }

return nil;

}

Thanks

1

There are 1 answers

2
Daniel S. On BEST ANSWER

The problem is how you access the assets-library URL. This is not a standard URL and loading the data from an assets-library URL does not work like this. Here is an example how to do it: Getting NSData from an NSURL

The following code is based on the above example. I'm not an expert in ALAssetLibrary, so try it with care. It needs quite some code to make the asynchronous working of the asset library synchronous again:

- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path{

    HTTPLogTrace();

    if ([path isEqualToString:@"/testPhoto"]){
        NSData* __block data = nil;

        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        dispatch_queue_t queue = dispatch_get_main_queue();

        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
        {
            ALAssetRepresentation *rep;
            if([myasset defaultRepresentation] == nil) {
                return;
            } else {
                rep = [myasset defaultRepresentation];
            }
            CGImageRef iref = [rep fullResolutionImage];

            dispatch_sync(queue, ^{
                UIImage *myImage = [UIImage imageWithCGImage:iref];
                *data = UIImageJPEGRepresentation(myImage, 1);
            });

            dispatch_semaphore_signal(sema);
        };
        ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
        {
            NSLog(@"Cant get image - %@",[myerror localizedDescription]);

            dispatch_semaphore_signal(sema);
        };

        NSString *testPath = @"assets-library://asset/asset.JPG?id=DB96E240-8760-4FD6-B8B4-FEF3F61793B3&ext=JPG";
        NSURL *deviceImageUrl = [[NSURL alloc] initWithString:testPath];
        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; //usin ARC , you have to declare ALAssetsLibrary as member variable
        [assetslibrary assetForURL:deviceImageUrl
                       resultBlock:resultblock
                      failureBlock:failureblock];

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

        dispatch_release(sema);


        HTTPDataResponse *photoResponse = [[HTTPDataResponse alloc] initWithData:imageData];
        return photoResponse;
    }
    return nil;
}