I am trying to convert copied images on the iOS pasteboard from a string to an image. 90% of the time this works under the scheme i have with URL's. However 1/10 times the image is not a url but is of what seems to be a base 64 encoding. The string looks like this.
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMSEBIPExMVFRAVEBAQEBYQFRUVDxAQFREWFhUVFRUYHSggGBolHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGhAQGy0lICUtLS0wLS8tLS0tLS0tLS8tLS0tLS0tLS0tLS0tLi0tLS0tLSstLS4tLS0tLSstLS0tLf/AABEIALcBEwMBIgACEQEDEQH/xAAbAAABBQEBAAAAAAAAAAAAAAADAAECBAUGB//EAEIQAAEDAwIDBQYDBgQEBwAAAAEAAhEDBCExUQUSQRMiYXGBBhQykaGxQsHwBxVSYnPRIzNygpKz4fEXJFNkorLS/8QAGgEAAwEBAQEAAAAAAAAAAAAAAQIDAAQFBv/EAC8RAAICAQIEAgoDAQEAAAAAAAABAhEDEiEEEzFRQbEFIjJhcZHB0eHwFIGh8UL/2gAMAwEAAhEDEQA/AO2YjNWPSvlYbfBfOaGe22majSpgrMF6FIXo3W0sTSafMlzLPF2N1IXQ3QoHLL8pwVQFyN1MV0KNy2XQU4KqdunFdYXQy4CnBVUVlIVkBXBloOUw9U+3TisgLoZeD1MPVEVlMVlhXAvCopCoqIrKQrLCOBd7RN2iqdql2q1g0Fo1FA1FX7VN2iwdAcvUC9BL0xesMohS5RLkPmTF6I2knKaVDnTc6waCSlKFzJ+ZENE5SJQ+ZKVjUOUNykSoErUMgZSTlJChzl6Vg5WGWRWy0BTwq81m0oxvdHJjauW1AT8oQ5jDRhG2cnFu9bvZhSFILcxmtGIy3cjNpOWuKITiihrBqRkcjku8tnsExt0HP3B5iMgPcpio7ZaJt0vd0uv3B1ozu1dsnbVdstEWymLUIORubEzxVKftitAWYU22KXUDmwKNOoSrlK2qO0atawsAMxlabWwuvh+DnmWpukcuTiUnUUcxUtao/CfRVajnDUFdkQql5aNcNE+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ukJkK0SMkkg+DYjEYESJ2KIxxIEuzMGMwYznCE74onEBM+qBIwAc9UwrAXLIIkjAB8jGh8UAMEidNW4yJzpOdVbZTBP4YiTHVRdTAyP9o6BU1MTSiDHAHIE41AaI6ERqoEDEHIxHT0RXsxOpQ+zODtr6rIxB7z/1P3TsqxJ18B1xrk/qVGqNY8spNGMjER67phSTnnq3p4SPTpr9VB0SHRH0MqT3QBgdNNVF1XyGNtfNMrQrpgvefA/VJOSTnRJPqiJpZWbUPTRSaJ6qsyqjU3KggcslM6niEmuUi5AZEOzCcUgkHJuZAIixGt7ca9eiC10qzSJBSz2Q0N2SFHMozaQj7IRcpseSueSZ0JoTaIB+6J2KEapBTseRlI0x00TeTGY0WW6mSVarV5KEHQrQjROUkwYpiM9EANVprxMnZBqJ0nYsmq2K7GGVYbRG+ZTsaEZjQjIEF3AimQiMCMGBHp0xEqbZSrKhONVetHQJKE5jSnpjELTVxFW0izUIgmdUmkxk4Q+QwEziQIKikUbCEAmVCpS8QfNQFQAKDHyU4hJzcQMeScvIAkIbjAU2PkaogGbWORCTavh1RGviEqhRABc/qAk+v03+QRJGqjduDtBAj6ogBjIlQGnRRnEJgcIgGNUJkPkKSNi0UaZRw9OkrkSbUZqSSDGQxdCTjhOkggsjTRqZJTpIS6Gh1LDW4SmEklzHUJplQrVOiZJGK3BJ7AAniUklayaREsTCmkkinsK1uPypuqSSUeg0wFHtCkktQWx+dPTqwUkln0FvoaDKuNFGq+eiSS5ki7BBkqYpDVJJGwA30ZQjSKSSMWLJEhTKlyHdJJNYtCcxMWpJJrBRXe3qq5fASSRQrBGoU6SS1go//9k=
Now I use the following code to recognize its base64 based off a post I found.
- (BOOL)isBase64Data :(NSString *)input{
input=[[input componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:#""];
if ([input length] % 4 == 0) {
static NSCharacterSet *invertedBase64CharacterSet = nil;
if (invertedBase64CharacterSet == nil) {
invertedBase64CharacterSet = [[NSCharacterSet characterSetWithCharactersInString:#"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="]invertedSet];
}
return [input rangeOfCharacterFromSet:invertedBase64CharacterSet options:NSLiteralSearch].location == NSNotFound;
}
return NO;
}
However it always returns false. Does this string I show need some trimming in some way? I find the length of the string to be 8391 therefore modulo 4==0 is never invoked.
/9j/4AAQSkZJ is a typical beginning of JPEG data, converted to hex as FFD8FFE00010+JFIF. If you want to convert it to an image, you need to point the converter to the point AFTER the data:image/jpeg;base64, text.
I think you can do something like this:
NSData *base64 = [yourData subdataWithRange:NSMakeRange(24, yourData.length-24)];
NSData *imgData = [[NSData alloc] initWithBase64EncodedData:base64 options:0];
UIImage *img = [UIImage imageWithData:imgData];
I might be wrong with the exact implementation but this is the general idea...
Related
I need some help:
i get from WebService only a part from the uunicode value, and after this I append the prefix \u to finish the value. The .ttf is good, i tested with some hardcoded values.
NSString *cuvant = [[self.catData objectAtIndex:indexPath.row]objectAtIndex:9]; //Get data
//apend prefix (double \ to escape the \u command)
cuvant = [NSString stringWithFormat:#"\\u%#",cuvant];
// cell.catChar.text = [NSString stringWithUTF8String:"\ue674"]; --->this works very well
cell.catChar.text = [NSString stringWithUTF8String:[cuvant UTF8String]]; //---> this doesn't work
i searched the documentation, and other sites but i didn't found nothing usefull, all the hints are with hardcoded data... i need tot take the codes dinamically
Thanks!
All you need is just to feed this unicoded string as data first. So make a C-String then
NSData *dataFromUnicodedString = [NSData dataWithBytes:yourCUnicodedString length:strlen(yourCUnicodedString)];
and afterwards the resulting string will be
NSString *unicodedString = [[NSString alloc] initWithData:dataFromUnicodedString encoding:NSUTF8StringEncoding];
Note: in other questions they compare a value stored in NSData objects, not its bytes.
I want to perform something like this:
NSData *d = ...;
if (d == "fff1") {
...
}
The only solution I have found:
NSData *d = ...;
NSString *str = [NSString withFormat:#"%#", d];
if ([str isEqualToString:#"<fff1>"] {
...
}
But I don't like that I need to add extra surrounding backets in comparison. Are there better solutions?
For purpose of comparing raw data you use memcmp:
NSData *dataA;
void *someBuffer;
if(memcmp([dataA bytes], someBuffer, dataA.length) == 0) ; //they are the same
Note you should watch that length is not too large for any of the buffers.
EDIT: added NSData procedure:
Or better yet you could convert your string to NSData and do the comparison on the NSData:
NSData *d = ...;
if([d isEqualToData:[NSData dataWithBytes:"fff1" length:sizeof("fff1")]]) {
}
I'm trying to display an image from my Database.
This part works nicely.
I save the image in the DB like this :
UIImage *resizedImg = [Generics scaleImage:self.photo.image toResolution:self.photo.frame.size.width and:self.photo.frame.size.height];
NSData *imgData = UIImageJPEGRepresentation(resizedImg, 0.9);
NSString *stringDataImage = [NSString stringWithFormat:#"%#",imgData];
[dict setObject:stringDataImage for key#"image"];
// POST Request to save the image in DB ...
Now when I try to load the image and set it into my UIImageView I do this way :
NSData *data = [[[MyUser sharedUser] picture] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
Or
NSData *data = [[[MyUser sharedUser] picture] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
self.imageView.image = [UIImage withData:data];
But this doesn't work.
data is not equal to imgData, I think it's the encoding but I can find the good one.
And [UIImage withData:data] return nil;
Can you help me?
EDIT :
Convert and save
UIImage *resizedImg = [Generics scaleImage:self.photo.image toResolution:self.photo.frame.size.width and:self.photo.frame.size.height];
NSData *imgData = UIImageJPEGRepresentation(resizedImg, 0.9);
[dict setObject:[imgData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength] forKey:#"image"];
Load the image
NSData *data = [[NSData alloc] initWithBase64EncodedData:[[MyUser sharedUser] picture] options:kNilOptions];
NSLog(#"%#", data);
self.image.image = [UIImage imageWithData:data];
You are converting the NSData to a string and saving that to your database. Two issues:
Your choice of using the stringWithFormat construct is an inefficient string representation of your data (resulting in string representation that is roughly twice the size). You probably want to use base64 (for which the string representation is only 33% larger).
You are saving your string representation, but never converting it back to binary format after retrieving it. You could write a routine to do this, but it's better to just use established base64 formats.
If you want to save the image as a string in your database, you should use base64. Historically we would have directed you to one of the many third party libraries (see How do I do base64 encoding on iphone-sdk?) for converting from binary data to base64 string (and back), or, iOS 7 now has native base 64 encoding (and exposes the private iOS 4 method, in case you need to support earlier versions of iOS).
Thus to convert NSData to NSString base64 representation:
NSString *string;
if ([data respondsToSelector:#selector(base64EncodedStringWithOptions:)]) {
string = [data base64EncodedStringWithOptions:kNilOptions]; // iOS 7+
} else {
string = [data base64Encoding]; // pre iOS7
}
And to convert base64 string back to NSData:
NSData *data;
if ([NSData instancesRespondToSelector:#selector(initWithBase64EncodedString:options:)]) {
data = [[NSData alloc] initWithBase64EncodedString:string options:kNilOptions]; // iOS 7+
} else {
data = [[NSData alloc] initWithBase64Encoding:string]; // pre iOS7
}
If you really want to turn binary data into a string you should be using base64 encoding. Luckily for you, NSData now supports Base64 natively
So you could get your string from data with:
NSString *stringDataImage = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
And you could turn this back into NSData with:
NSData *imgData = [[NSData alloc] initWithBase64EncodedString:stringDataImage options:kNilOptions];
I am trying to extract the URL for an image from a string of text. It may be from different websites, and could be any form of image (jpg, png, gif, etc.). What would be a good way to scan the string, find any matching picture extension, and get the graphic from it?
The string may be something like this:
Hello, I hope you like my picture located at http://www.website.com/picture1.png. However, if you don't, I know you'll like this one http://www.website2.org/picture2.jpg.Please refer all comments to http://www.website5.com/
I want to be able to scan the string for ONLY URLs of image files, and make a new string of whatever the first image URL is. So in this string, I could create a new string that only has http://www.website.com/picture1.png
I got from The Link Objective-C - Finding a URL within a string
No need to use RegexKitLite for this, since iOS 4 Apple provide NSDataDetector (a subclass of NSRegularExpression).
You can use it simply like this (source is your string) :
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray* matches = [detector matchesInString:source options:0 range:NSMakeRange(0, [source length])];
Use below code after later finish with #Anup Kumar
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match range];
if ([match resultType] == NSTextCheckingTypeLink) {
NSURL *url = [match URL];
} else if ([match resultType] == NSTextCheckingTypePhoneNumber) {
NSString *phoneNumber = [match phoneNumber];
}
}
Well, to get the extension you can use the NSString method pathExtension, this will result in a string with just the extension, so you will know if it is jpg, gif, png, etc.
NSString* path = #"http://location.com/pictures/image.jpg";
NSString* extension = [path pathExtension];
For your graphic you can use the UIImage imageWithData method, and pass it the output from NSData dataWithContentsOfUrl method, using these methods together will allow you to create a UIImage which you can then display in a UIImageView.
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:path]];
Hopefully that is helpful.
When I scan the barcode and I get some value if it is Equal=2 then I need to display with == and if it is Equal=3 then I need to display with = and if the value is 4 then invalid.
But Scanned Barcode are of integer value -- when decode using NSASCII it is displaying only till value 127 after that it is showing invalid results. Eg: if my Barcode value = 9699 the result value=jem then my added result value=jem= actualstring value=%åasc value id only showing 37
Here is my code:
- (void) readerView:(ZBarReaderView *)view didReadSymbols:(ZBarSymbolSet *)syms fromImage:(UIImage *)img
{
// do something useful with results -- cool thing is that you get access to the image too
for (ZBarSymbol *symbol in syms) {
[resultsBox setText:symbol.data];
if ([resultsBox.text length] == 2) {
addedresult.text = [resultsBox.text stringByAppendingString:#"=="];
} else if ([resultsBox.text length] == 3) {
addedresult.text = [resultsBox.text stringByAppendingString:#"="];
} if ([resultsBox.text length] >= 4) {
addedresult.text = #"Invalid";
}
[Base64 initialize];
NSString *myString = [[NSString alloc]initWithString:addedresult.text];
NSData * data = [Base64 decode:myString];
NSString * actualString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"%#",actualString);
labeltext.text= actualString;
int asc = [actualString characterAtIndex:0];
label.text = [NSString stringWithFormat:#"%d", asc];
[actualString release];
break;
}
}
Since someone revived this question's comments, i'll revive this entire post.
You shouldn't go through NSData to create an NSString from something you already have, and you're probably losing something along the way. Go directly to NSString using stringWithFormat. Also, ASCII will come back and byte you later, if you have a choice, use UTF8.
NSString *actualStringUTF8 = [NSString stringWithFormat:#"%#",[addedresult.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSString *actualStringASCII = [NSString stringWithFormat:#"%#",[addedresult.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",actualStringUTF8);
NSLog(#"%c",[actualStringUTF8 UTF8String]); //This is a const char*
Secondly, I looked into the SDK and it says symbol.data is already an NSString*. Depending on what you want, you may not need to do anything. If you do end up needing to change encoding, make sure you understand why you need to (one good reason is "the rest of the application uses NS****StringEncoding").
Also make sure you compare strings the correct "Objective-C" way:
[actualString isEqualToString: testString];
NOT actualString == testString;