Reading picture on iOS in Cocoa - ios

I am getting such kind of URL for pictures on my iPad:assets-library://asset/asset.JPG?id=00000000-0000-0000-0000-000000000BC4&ext=JPG
How can I read binary the content of the picture ?Opening the file with, for example, stringFromFileAtURL gives me that path is not found.Thanks.

You are not allowed to read stuff from there as you please, because it is outside your app's sandbox.
This is not going to be as simple as you want it to be. First you must get the actual image file. This involves getting an ALAsset object (you can see this process in this question. Then, you get that asset's defaultRepresentation and then you get the representation's fullResolutionImage. Then you have a CGImageRef, and you can get is data provider via CGImageGetDataProvider and then a copy of the pixel data via CGDataProviderCopyData which is a CFDataRef (you can cast it to NSData *).
Do you really need the binary? Or is a UIImage good enough? Not sure of your intent with this.

It is not necessary to use the assets library, UIImage is the binary representation. If all you want to do is save the image as a file use
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

Related

Control over format when using RequestImageFileAsync in Blazor WebAssembly

Blazor Web assembly has a convenience method that converts an IBrowserFile containing an image into a resized version - which is handy for resizing large images prior to uploading them.
This method takes a format as a string which determines the format of the output file.
Is there anywhere a list of valid formats that this property will accept? Can you specify the compression or bit depth values on the resulting file?
Currently, If I take an existing .jpg file and convert it using a format string of "jpg" the resulting file, although smaller in pixel dimensions is actually about double the size on disk. A 4000x3000 image at about 2.8MB can be "reduced" to a 2000x1500 image that's 7.7MB in size. Which is obviously not helping when the purpose is to reduce upload size. I could easily upload the 2.8MB file and resize it more efficiently on the server.
var imageFile = await file.RequestImageFileAsync("jpg", 2000, 2000);
This suggests I'm using the method incorrectly - but Microsoft's documentation on this method gives no clues as to what valid "Format" strings might, only stating that it is a string type. I've tried ".jpg", "JPEG", "jpg" - all of which seem to produce the same valid jpg file. What should I be passing here to actually reduce the file size?
See https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types
It's actually not "image/jpg" but "image/jpeg". If you specify non-existent format, the fallback (at least for me) seems to be "image/png". That's why you always got a valid image but with the same filesize.
I think this method uses html types:
html types
Try "image/jpg".
Be careful, though, this is a request to the browser, and the browser can send back whatever it wants. I believe this will work fine on all browsers, but you'd better check some of the common culprits (Hi, Opera!) to confirm.

convert online image uri into base64 with flutter

i read this post convert image to base64 with flutter but this is about converting image file to base64. How to convert online url to base64.
Right now i can think of one solution that is to store image in path and get that file path and convert it. thats how this post showing.
is there any other sort way to convert online image uri to base64?
Well to convert the image you need the image data otherwise there's nothing to convert. So the step for the most effecient would be.
Perform get request to the image url
Read all the bytes from the response body
Convert to base64
Save the base 64 string locally or use how you please.
You don't have to save it. Just keep in mind if the user closes the app or it stops at any point during this process you'll have to start it from the beginning because you're not saving the image to disk.

UIImage to NSURL

I have generated an image in my app an I would like to share it via UIDocumentInteractionController which takes an argument in form of NSURL. Of course I don't want do directly convert image data into a URL, but what is the best way to achieve this? Can you temporarily store that image somewhere and then get the url and delete it afterwards after UIDocumentInteractionController has done it's job? Or convert it to NSData somehow and then get the url of the NSData object? (I tried this and failed btw)

How to read binary QR Code with AVFoundation?

Is it possible to read a binary encoded QR Code with AVFoundation?
I can get a AVMetadataMachineReadableCodeObject object of .type AVMetadataObjectTypeQRCode, however this only has a stringValue property, which won't work, because the data contained in the QR Code can't be converted to a string friendly representation.
Should I use ZXing instead?
Thanks.
The raw data does exist in your AVMetadataMachineReadableCodeObject, but it's not available through a public getter.
However, you can use KVO to extract it, but Apple might reject your app. Also, future iOS versions might change their private APIs and your code could become invalid (because of the hardcoded private keys).
Swift:
readableCodeObject.valueForKeyPath("_internal.basicDescriptor")!["BarcodeRawData"]
Objective-C
[readableCodeObject valueForKeyPath:#"_internal.basicDescriptor"][#"BarcodeRawData"];
I tested this for iOS 8 and 9.
I was able to solve this issue by Base64 encoding the data in the QR code.
This obviously won't work if you're not also generating the QR codes but could be option for people that are.
We were running into the upper limit of data that can be stored in a QR code but by compressing the data (we used zlib) and then Base64 encoding the compressed data, so long as your data compresses to less than 75% of its original size you get some additional capacity and can use the stringValue property to get your data back out, you just have to Base64 decode and then decompress to get the original data back.
Even if you're starting with binary data that isn't very compressible, so long as you can handle the overhead of Base64 and still be within the limitations of QR codes this may be a viable option that avoids working around the fact that AVMetadataMachineReadableCodeObject seems to want to work with string values.
You can use a CIDetector to get to a CIQRCodeFeature which has a symbolDescriptor which has a errorCorrectedPayload which contains the data.
Only problem is that this data still includes QR code headers, like ECI etc... so you still need to interpret the bits.
I summed it up in a post here.
Inspired by previous answers and other sites, I have created a gist that allows to extract binary from QR code or Aztec code, without using private APIs nor other library. It is a AVMetadataMachineReadableCodeObject extension presenting a binaryValue.
However, it only runs on iOS 11 and later, because of the CIQRCodeDescriptor use.
It is available here : https://gist.github.com/PetrusM/267e2ee8c1d8b5dca17eac085afa7d7c
For QR codes, it works only with 100% binary ones. But if they contain further parts, you can easily adapt it.

How can I load a .webarchive into a UIWebView from an in-memory NSData?

I have an NSData containing a .webarchive blob that I'd like to load into a UIWebView. I know that this is possible (see this question), and I have it working if I first serialize it to disk and then load it with UIWebView's -loadRequest: method.
However, I'd prefer not to serialize to disk first since I already have the data in memory. I've tried to use -loadData:MIMEType:textEncodingName:baseURL: with the data, and various base URLs, but it always fails (nil, #"http://", the actual root path that the web archive contains, etc) to load.
Again, the same archive loads correctly if I bounce it to disk first and load via -loadRequest:, so I feel like something about the MIMEType (I'm using application/octet-stream) and/or the base URL is wrong. Anyone know what the incantation is?
Using -loadData:... will work. The MIME type specified must be application/x-webarchive (not the generic "octet-stream"). If this is set correctly, both the text encoding and base URL can be just supplied as nil.

Resources