Change UIImage imageOrientation metadata without affecting the image - objective-c

I have an app which loads an image from the local disk and then displays it on a UIImageView. I want to set the image to aspect scale to fit.
The problem I'm having is that the imageOrientation is coming back as UIImageOrientationRight even though it's a portrait image and that's messing with how the aspect calculations are done.
I've tried a few methods of changing the meta data but both rotate the image when it gets displayed.
UIImageView *iv = [[UIImageView alloc] initWithFrame:self.frame.frame];
NSMutableString *path =[[NSMutableString alloc] initWithString: [[NSBundle mainBundle] resourcePath]];
[path appendString:#"/pic2.jpg"];
NSData *data = [NSData dataWithContentsOfFile:path];
UIImage *img = [UIImage imageWithData:data];
UIImage *fixed1 = [UIImage imageWithCGImage:[img CGImage]
scale:1.0
orientation: UIImageOrientationUp];
UIImage *sourceImage = img;
UIImage *fixed2 = [UIImage
imageWithCGImage:[img imageRotatedByDegrees:90].CGImage
scale:sourceImage.scale
orientation:UIImageOrientationUp];
iv.image = fixed1; // fixed2;
[iv setContentMode:UIViewContentModeScaleAspectFill];
[self.view addSubview:iv];

In the end I took a different approach and made the UIImageView 360 wide and placed it in the centre and this achieved the desired effect.

Related

Animating images Objective-C

I'm using an animation block to iterate through an array of .png images to make it look like a character is walking across the screen. However, the quality of the images blurs when I scale the image down. Currently I am testing the animation with 19 images all in 371 x 379 resolution. However, I need them to appear smaller on the screen so I scaled them to 64 x 65 on my app. When I do this, the lines distort a little bit and when I pause the game you can really tell that the image quality isn't where it should be. Below is the code I'm using to iterate through the images. Any help would be greatly appreciated.
[UIImageView animaiteWithDuration: 3.0
delay: 0
options: UIViewAnimationOptionBeginFromCurrentState
animations: ^{
CABasicAnimation *mover = [CABasicAnimation animationWithKeyPath:#"position"];
// Get the current presentationLayer of the object
CALayer *currentLayer = self.layer.presentationLayer;
// Get the current point of the presentationLayer
CGPoint currentPoint;
currentPoint.x = currentLayer.position.x;
currentPoint.y = currentLayer.position.y;
// Set the new point for the object to move to
CGPoint newPoint;
newPoint.x = currentLayer.position.x - 50;
newPoint.x = currentLayer.position.y;
// Set the from and to values of the animation
[mover setFromValue: [NSValue valueWithCGPoint:currentPoint]];
[mover setToValue: [NSValue valueWithCGPoint:newPoint]];
[mover setDuration:3.0]; // Set the duration of the mover animation
// Create the nine-teen UIImages for the array
UIImage *image1 = [UIImage imageNamed:#"img1"];
UIImage *image2 = [UIImage imageNamed:#"img2"];
UIImage *image3 = [UIImage imageNamed:#"img3"];
UIImage *image4 = [UIImage imageNamed:#"img4"];
UIImage *image5 = [UIImage imageNamed:#"img5"];
UIImage *image6 = [UIImage imageNamed:#"img6"];
UIImage *image7 = [UIImage imageNamed:#"img7"];
UIImage *image8 = [UIImage imageNamed:#"img8"];
UIImage *image9 = [UIImage imageNamed:#"img9"];
UIImage *image10 = [UIImage imageNamed:#"img10"];
UIImage *image11 = [UIImage imageNamed:#"img11"];
UIImage *image12 = [UIImage imageNamed:#"img12"];
UIImage *image13 = [UIImage imageNamed:#"img13"];
UIImage *image14 = [UIImage imageNamed:#"img14"];
UIImage *image15 = [UIImage imageNamed:#"img15"];
UIImage *image16 = [UIImage imageNamed:#"img16"];
UIImage *image17 = [UIImage imageNamed:#"img17"];
UIImage *image18 = [UIImage imageNamed:#"img18"];
UIImage *image19 = [UIImage imageNamed:#"img19"];
// Populate the animation array
NSArray *walkingAnimation = [NSArray arrayWithObjects: img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16, img17, img18, img19, nil];
[self setAnimationImages: walkingAnimation]; // Set the animation images to the array
[self setAnimationRepeatCount:3]; // Repeat the animation every second
[self setAnimationDuration:1.0];
[self startAnimating]; // Start the animation
[[self layer] addAnimation:mover forKey:#"position"]; // Add the mover animation to the layer
[UIImageView commitAnimations];
[[self layer] setPosition:newPoint]; // Set the position to avoid bounce back
} completion:nil];
So if anyone can tell me if there is something extra I need to be doing to keep the images from distorting, or maybe it's the images themselves, I could really use some tips while I read everything in Apples API and check every forum on the internet.
Your issue should be nothing to do with code. You are resizing the images to a different aspect ratio. You should be very careful with image resizing as the image will naturally blur.
Your best option is to create accurate (sharp) images, purpose made for the size at which you will use them.

Showing only a portion of the original image in a UIImageView

How can i show only a portion of the original image in a UIImageView
This question may be very familiar and old, But the reason for asking again is,i could not find a workable idea with the help of those answers
Many said set image.contentMode = UIViewContentModeCenter; (but not working)
I need almost a rectangle containing the center of the original image,How do I get this ?
I do make this working,when i am displaying a static image to my app and setting Content mode of UIImageVie as Aspect Fill.
But this is not workable in the case when i am displaying an image from url and using NSData
Adding my code and the images
NSString *weburl = #"http://topnews.in/files/Sachin-Tendulkar_15.jpg";
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 108, 86)];
NSURL *url = [NSURL URLWithString:weburl];
NSData *data = [NSData dataWithContentsOfURL:url];
imageView.image = [UIImage imageWithData:data];
[self.view addSubview:imageView];
If you added the UIImageView form XIB, you can find a "mode" property there and then you can see and set different modes from there (from Interface builder). Or by programatically, you can set different modes by
imageView.contentMode = UIViewContentModeCenter;
imageView.clipsToBounds = YES;
Try this:
self.imageView.layer.contentsRect = CGRectMake(0.25, 0.25, 0.5, 0.5);
self.imageView will display middle part of image. You can calculate itself the required values of CGRect ​
For this kind of output, you need to crop the image according to your requirement.
Cropping code as below which can be used.
-(UIImage *) CropImageFromTop:(UIImage *)image
{
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, 12, image.size.width, image.size.height - 12));
UIImage *cropimage = [[[UIImage alloc] initWithCGImage:imageRef] autorelease];
CGImageRelease(imageRef);
return cropimage;
}
you try to scale image and then add image in UiImageView and set center that image then it is in center. code of scale image is
-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
then you set the center of that image and add in the image view i hope this is work

How to add image from url to MKAnnotationView for retina display?

I'm trying to create a custom MKAnnotationView with image from a url.
What happens is that when the device has retina display, the image of the MKAnnotationView is blurred, as it double its resolution.
If the image is from the app, it will load the #2x image (if one exists), but if you set an image from a url like this for example:
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation
{
MKAnnotationView *customAnnotationView=[[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:nil];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:#"http://www.interaction-design.org/images/icons/play-button-red-300x300.png"]];
UIImage *img = [UIImage imageWithData:imageData];
[customAnnotationView setImage:img ];
return customAnnotationView;
}
you will see the image on an retina display very pixelated.
Any advice what to do?
Thanks
The gist is this...
Use [[UIScreen mainScreen] scale] to ask your server for a standard or #2x image.
Create a CGImageRef from the NSData
Use the CGImageRef and [[UIScreen mainScreen] scale] to create a UIImage
The code to do that looks like this:
NSString *imagePath = #"http://www.interaction-design.org/images/icons/play-button-red-300x300";
imagePath = [imagePath stringByAppendingString:[[UIScreen mainScreen] scale] > 1 ? #"#2x.png": #".png"];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:imagePath]];
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)imageData);
CGImageRef imageRef = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);
UIImage *image = [UIImage imageWithCGImage:imageRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];
CGDataProviderRelease(dataProvider);
CGImageRelease(imageRef);
Just note that you will need to have two resolutions of the image on the server given the first two lines of my code. Currently the image will appear smaller on retina, larger on non retina screens.

Show an image from json

I'm not able to show an image that I load from a json file.
I'm parsing my json with JSONKit and everything works fine but I can't load an image in the UIImageview. I hope some of you can help me out there.
below my code I thought might be correct but it isn't.
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 115)];
image.image = [UIImage imageNamed:[detailView objectForKey:#"thumbnail"]];
[self.view addSubview:image];
[image release];
i think in json u are getting url of image. display image from url using below code
//NSURL *url = [NSURL URLWithString:#"http://192.168.1.2x0/pic/LC.jpg"];
NSURL *url = [NSURL URLWithString:[detailView objectForKey:#"thumbnail"]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 460.0f)];
[subview setImage:[UIImage imageWithData:data]];
[self.view addSubview:subview];
[subview release];
the code you posted
image.image = [UIImage imageNamed:[detailView objectForKey:#"thumbnail"]];
will try to find the image on your bundle named the string stored in [detailView objectForKey:#"thumbnail"]
As you mentioned, your images are from remote server, you have to download the image from your remote server.
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 115)];
image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[detailView objectForKey:#"thumbnail"]]]];
[self.view addSubview:image];
[image release]

Display NSImage from NSData

My target is to display an image in a view.
Considering I've an:
IBOutlet NSImageView *image for display my image
NSData *imageData for readig the image file
NSImage *imageView
In imageData is stored the image (I've used initWithContentsOfFile method).
Now, if I initialize imageView with:
NSImage *imageView = [[NSImage alloc] initWithContentsOfFile:path];
I can see the rendering of the image correctly, but in this way I'm reading twice from the file system.
If I try to:
NSImage *imageView = [[NSImage alloc] initWithData:imageData];
The displayed image is very small..like thumb
Whit CGImageRef:
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
CGImageRef imageRef= CGImageSourceCreateImageAtIndex(imageSource, 0, nil);
NSImage *imageView = [[NSImage alloc] initWithCGImage:imageRef size:NSZeroSize];
The image is still rendering too small.
How can I display the image in original resolution?
NSImage *imageView = [[NSImage alloc] initWithData:(NSData *)imageData];