I'm working with an existing code base that accepts bytes from a loaded jpeg file and creates a UIImage object. That works fine on iphone but macOS needs a different implementation from what I've understood.
UIImage* image = [UIImage imageWithData:[NSData dataWithBytes:data length:length]];
if (image)
{
}
What is the equivalent of this on macOS? is it NsImage? How do I implement this?
Here it is
NSImage* image = [[NSImage alloc] initWithData:[NSData dataWithBytes:data length:length]];
if (image)
{
}
Related
I did some research and found that I should be able to convert a PDF into a byte[] and then into a UIImage but so far no luck. The reason for not being able to use CoreGraphics is that I'm using apportable which does not support the CGPDF classes.
I'm able to create a NSData object that seems to contain the PDF but when using that object to create a UIImage the image is nil.
- (instancetype) initWithBackSize: (CGSize) size
{
NSString *path = [[ NSBundle mainBundle ] pathForResource:#"pdfTest.pdf" ofType:nil ];
NSData* pdfData = [NSData dataWithContentsOfFile:path];
UIImage* image = [[UIImage alloc] initWithData:pdfData];
CCTexture* texture = [[CCTexture alloc] initWithCGImage: image.CGImage contentScale: image.scale];
self = [super initWithTexture: texture];
return self;
}
The binary data within a PDF file cannot be translated directly into an UIImage type. You need to find a third party library or some other way of converting a PDF file into an UIImage.
The app uses a series of jpg's and a timer that steps through them to make an animation.
During the animation on the device, it crashes (didReceiveMemoryWarningError.)
I'm new to iPhone programming and Objective-C. How can I optimize this flipbook for the iPhone?
I can imagine simply compressing the jpeg's and perhaps losing some quality would help, but my understanding is the iPhone does its own image compression/decompression on the device and I may be wasting my time.
Tried different things, eventually hit on storing an array of NSData objects and converting to UIImage on the fly.
for (NSString *imageFile in directoryList) {
NSString *fileLocation = [imageFolder stringByAppendingPathComponent:imageFile];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
if (imageData) {
[images addObject:imageData];
} else {
[Util trace:#"cannot get image data for image named: %#", fileLocation];
}
and then to update your image:
-(void)updateImageView:(id)sender
{
UIImage *anImage = [UIImage imageWithData:[images safeObjectAtIndex:nextImage] ];
[imageView setImage:anImage];
nextImage++;
}
I am trying to update a UIImage with geotag information. I looked at Saving Geotag info with photo on iOS4.1, which is where I found a reference to the NSMutableDDictionary+ImageMetadata category. However, I don't want to save to the photo album, but have a UIImage to pass on.
The following code seemed like it was making too many copies of the image and required all these frameworks linked: CoreImage, AssetsLibrary, CoreMedia, ImageIO.
Is there something more efficient that UIImage -> CIImage -> CGImage -> UIImage that can take the properties NSDictionary needed for setting EXIF data?
- (UIImage *)updateImage:(UIImage *)image location:(CLLocation *)location dateOriginal:(NSDate *)dateOriginal
{
NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary:[image.CIImage properties]];
// uses https://github.com/gpambrozio/GusUtils
[properties setLocation:location];
[properties setDateOriginal:dateOriginal];
CIImage *tempImage = [[CIImage alloc] initWithImage:image options:properties];
CIContext *context = [CIContext contextWithOptions:nil];
UIImage *updatedImage = [UIImage imageWithCGImage:[context createCGImage:tempImage fromRect:tempImage.extent]];
return updatedImage;
}
Have a look at libexif: http://libexif.sourceforge.net/
You'll probably need to pass the image's byte data to the library using
UIImageJPEGRepresentation(image, quality) bytes]
I had a lot of trouble trying to force the loading of the low-resolution version of some resources on iOS, when the high-res version (#2x) is also present.
The reason I wanted to do this is simple: My app shows a bunch of full-screen images to the user (320x480 on older devices, 640x960 on Retina devices), and the user can upload some of these to TwitPic. I wanted to unify the uploaded images to 320x480, regardless of the device (for consistency, because that size is fine in a web browser).
I found out that no matter what UIImage method I used, when the high-resolution version of a resource file is present it is loaded (even if you pass the full file path): UIImage will out-smart you. But I found a way to out-smart UIImage:
- (UIImage*) lowResImage{
NSString* path = [[NSBundle mainBundle] pathForResource:_fileName
ofType:#"png"
inDirectory:_resourceSubdirectory];
path = [path stringByReplacingOccurrencesOfString:#"##2x" withString:#""];
NSData* imageData = [[NSData alloc] initWithContentsOfFile:path];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[imageData release];
return [image autorelease];
}
The above code works fine on a 4th generation iPod touch.
Anybody came up with a better approach?
Build your path, open data, and then init the image using data as in the example below.
NSString *dataPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:_fileName];
NSData *data = [[NSData alloc] initWithContentsOfFile:dataPath];
UIImage *image3 = [UIImage imageWithData:data];
NSData *imageData = UIImagePNGRepresentation(your_image_here);
After that i transferred it to another iPhone using bluetooth.
Now i need to convert the data back to image. Can any one tell me how to do it?
I assume this is to et back the data which you encoded in the previous question.
For converting UIImage to NSData ,
NSData* pictureData = UIImagePNGRepresentation(image);
To get it back,
UIImage *image = [[UIImage alloc]initWithData:pictureData];
Remember to add [image release] at the end.
or you can use
UIImage *image = [[UIImage alloc]init];
[image imageWithData:pictureData];
In swift-3 : Data is converted back to image by :--
func compressImage() -> UIImage {
let imageData = UIImageJPEGRepresentation(UIImage(named:"background.jpg")!, 0.2)
let image = UIImage(data: imageData!)
return image!
}
Just call imageWithData: or on instantiation, you can call initWithData:
[UIImage imageWithData:(NSData *)data];