iOS Question Return raw data address

Biswajit

Active Member
Licensed User
Longtime User
Hello, I'm creating a wrapper for an image processing library.

Some of the properties return (void *) data type.

My question is how can I return this data from a wrapper so that I can send it as a parameter to the Objective C methods from B4I.

1662484990832.png


I wrapped the void pointer with NSData. And then wrapped it with B4IArray. But when I am trying to get the buffer in the ObjC code like b4iarray.bytesData.bytes that is showing a broken image.

Objective-C:
- (B4IArray*)data{
    NSData* data = [NSData dataWithBytes:[buff data] length:[buff size]];
    B4IArray* arr = [B4IArray new];
    arr = [arr initBytesWithData:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
    return arr;
}

Is there any specific way to wrap the void pointer in B4I or not? If not then how to access this type of data?
 
Last edited:
Solution
B4X:
 NSData* data = [NSData dataWithBytes:[buff data] length:[buff size]];
B4IArray *arr = [B4IArray new];
 arr.bytesData = data;

JordiCP

Expert
Licensed User
Longtime User
Using a void pointer in C is a safe way to provide a memory address that points to data which can be in several formats, depending on the use case.
The 'user' of this pointer is supposed to know (by means of other object's properties or the specific context) the specific data format.

I'd try to cast it to a long (NSInteger) for B4i, and cast it again to (void*) data in the OBJC side. Or, in the wrapper, create a B4i array from this data, assuming that the type is known, and return it. No idea l if any of them will work, just a guess.
 
Upvote 0

Biswajit

Active Member
Licensed User
Longtime User
Yes, actually I wrapped the void pointer with NSData. And then wrapped it with B4IArray. But when I am trying to get the buffer in the ObjC code like b4iarray.bytesData.bytes that is showing a broken image.

That is why I asked if there is any specific way to wrap the void pointer in B4I or not. If not then how to access this type of data?
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Yes, actually I wrapped the void pointer with NSData. And then wrapped it with B4IArray. But when I am trying to get the buffer in the ObjC code like b4iarray.bytesData.bytes that is showing a broken image.
Well, I guess it depends on what a broken image means. If what you get is only part of the image but this part is correct or even skewed, perhaps the buffer size is not enough because not only width and height but also the stride has to be taken into account.
If the image somehow resembles the original one, but with switched colors, stripes and/or noise, then it will possibly mean that the data format is different.

Perhaps the data format can be retrieved from the original object. For instance, with openCV it can be 4-bytes per pixel BGRA (even though the colors can be switched by core functions) or even encoded into a float.
 
Upvote 0

Biswajit

Active Member
Licensed User
Longtime User
B4X:
 NSData* data = [NSData dataWithBytes:[buff data] length:[buff size]];
B4IArray *arr = [B4IArray new];
 arr.bytesData = data;
Thank you. It worked.
 
Upvote 0
Top