Can I get a gif out of a custom MKAnnotationView? - ios

I need to get gifs with different colors from this library: https://github.com/samvermette/SVPulsingAnnotationView
In the library I have access to the layer of the MKAnnotationView. But I need an animated gif of the generated annotation to use in a non-iOS project.

I used renderInContext to create pngs from the CALayer (result). Then I created the gifs using python.
UIGraphicsBeginImageContextWithOptions(result.bounds.size, result.opaque, 0.0);
[result renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *pngFilePath = [NSString stringWithFormat:#"%#/blue dot.png",docDir];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:YES];

Related

function to duplicate images for thumbnail?

I'm making a photo gallery app.
I'm compressing the images
NSData *imageData = UIImageJPEGRepresentation(image,0.5)
and then want to create a duplicate (for thumbnails) that I'll compress even further
NSData *imageData = UIImageJPEGRepresentation(image,0.2)
I'm a little stuck on the middle step of how to create a duplicate, anyone know if there is a function for this?
Probably you can convert NSData to UIImage.
Swift3:
let imageData = UIImageJPEGRepresentation(thumbImage,0.5)
let thumbImage = UIImage(data:UIImageJPEGRepresentation(UIImage(data: imageData),0.2))
Objective C:
NSData *imageData = UIImageJPEGRepresentation(image,0.5)
UIImage *thumb = [UIImage imageWithData: UIImageJPEGRepresentation([UIImage imageWithData: imageData],0.5)];

Unarchive UIImage object returns CGSizeZero image using NSKeyedUnarchiver on iOS 8

I have this code working on iOS 7:
NSData *imageData = [NSKeyedArchiver archivedDataWithRootObject:self.imageView.image];
UIImage *imageCopy = [NSKeyedUnarchiver unarchiveObjectWithData:imageData];
NSLog(#"%#",NSStringFromCGSize(imageCopy.size));
but on iOS 8, imageCopy's size is always zero. Same thing happens when I archive UIImageView, the unarchived imageView's image has a zero size. I found out that in iOS 7, UIImage header is like:
UIImage : NSObject <NSSecureCoding, NSCoding>
but on iOS 8 it is :
UIImage : NSObject <NSSecureCoding>
It looks like the NSCoding protocol is missing on iOS 8. I have to encode the actual image data: UIImagePNGRepresentation(self.imageView.image) instead of the image to make sure I get a good image back.
Does anyone know why this happens? Is it for backward compatibility? I noticed in iOS earlier version UIImage doesn't conform to NSCoding.
UIImage : NSObject <NSSecureCoding> is not a problem because NSSecureCoding inherits NSCoding.
Anyway, I confirmed the problem can be reproduced with following code:
UIImage *img = [UIImage imageNamed: #"myImage"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:img];
UIImage *imgCopy = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(#"%#, %#", imgCopy, NSStringFromCGSize(imgCopy.size)); // -> (null), {0, 0}
On the other hand, the following code works as expected:
UIImage *img = [UIImage imageNamed: #"myImage"];
UIImage *img2 = [UIImage imageWithCGImage:img.CGImage scale:img.scale orientation:img.imageOrientation];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:img2];
UIImage *imgCopy = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(#"%#, %#", imgCopy, NSStringFromCGSize(imgCopy.size)); // -> <UIImage: 0x7fa013e766f0>, {50, 53}
I don't know why, maybe bug?
I think, this is related to imageAsset or traitCollection property introduced in iOS8

Converting from JSON string to UIImage always gives null?

I Get this kind of JSON:
NOTE: I put dots in "content" because it is too long byte array abd just to explain the situation.
{
"id":"53abc6a7975a9c10c292f670",
"nfcId":"testse",
"company":"TESt",
"qrId":"testvalue",
"address":"ajs;ldfh",
"mimeType":"IMAGE",
"url":"",
"content":"iVBORw0KGgoAAAANSUhEUgAA....."
}
And im trying to get this Json and diplay this information
the "content" field has a Byte array converted on the server from Image to byte array.
I use this code in xCode to convert those bytes to NSData, then to UIImage to be able to display it in UIImageView:
NSData *dataImage = [jsonArray[key] dataUsingEncoding:NSUTF8StringEncoding];
NSLog(#"data = %#", dataImage);
UIImage *img = [UIImage imageWithData:dataImage];
NSLog(#"img = %#", img);
The image is always gives me null.Although, data give me array of data.
I tried all kinds of encodings as a NSData parameters also:
dataUsingEncoding:NSASCIIStringEncoding
dataUsingEncoding:NSUTF8StringEncoding
dataUsingEncoding:NSUTF16StringEncoding
dataUsingEncoding:NSUTF32StringEncoding
I've used code like this before
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"data:image/png;base64,%#",jsonArray[key]]];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:imageData];
Note that initWithBase64EncodedString is only available from iOS7 onwards
I tried this code just right now and it works:
NSData* dataImage = [[NSData alloc] initWithBase64EncodedString:jsonArray[key] options:0];
UIImage *img = [UIImage imageWithData:dataImage];
The "content" encoded by Base64 String type.

Save a UIImage and reused it on UIImageview

I currently have the ability to take a photo or load from the camera and place an image in the UIImageView in my app be i want the ability to have a button that will save the image so when i reload the app the image is still in the UIImageView.
My though was a button to save the image once you have loaded it and in the
-(void)viewDidLoad
have the code to load the image that was saved but i don't know how to approach this.
Any help would be fantastic.
You can convert images to NSData using either UIImagePNGRepresentation or UIImageJPGRepresentation, then save the data to file calling one of NSData's writeToFile... methods.
Then when you restart your application, you can get the image by calling [UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]]
-- EDIT --
Save image
UIImage *image = ...;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image.png"];
[UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES];
Load image
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image.png"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]];
I used NSCachesDirectory since I'm assuming you don't want to have this image backed up (either through iCloud or iTunes). If you do then you'd want to use NSDocumentDirectory instead.

UIImage is empty after calling imageWithContentsOfFile

As you can see, that I have put my the national flags in a folder in Xcode and I am trying to display it to the navigation bar. However, it is not showing up and I found out:
NSString *imageName = [NSString stringWithFormat:#"%#.icns",countryName];
UIImage *image = [UIImage imageWithContentsOfFile:imageName];
image is "nil".
Any idea? Thanks!
You could use like this
NSString *imageName = [NSString stringWithFormat:#"%#.icns",countryName];
UIImage *image = [UIImage imageNamed:imageName];
Please Try This
Check whether the file actually exists. I suspect it doesn't. Use [NSFileManager defaultManager] fileExistsAtPath:.
Where was the image path you are sending NSString to here
UIImage imageWithContentsOfFile:imageName
send the path to that method. or make like this
UIImage *image = [UIImage imageNamed:[NSString stringwithFormat:#"%#.icns",countryName]];

Resources