Can't center image in UIImageView programatically - objective-c

In UIView subclass init method I have this piece:
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 436)];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString: imageSource]]];
myImage = [[MyMath sharedMySingleton] imageWithImage:myImage convertToSize:CGSizeMake(320, 436)]; // resizes image to set bounds
imageView.contentMode = UIViewContentModeCenter;
[imageView setImage:myImage];
imageView.contentMode = UIViewContentModeCenter; should center image, but still it shows up in top left corner. What am I doing wrong?
EDIT: Sorry, i found answer myself. thanks to everyone

Your myImage size is equal to the size of imageView. To see the difference make your myImage size a bit smaller than that of imageView.After that it will be centered. In your one line change the size of image :
myImage = [[MyMath sharedMySingleton] imageWithImage:myImage convertToSize:CGSizeMake(200, 200)]; // resizes image to set bounds

Its because your imageView size and myImage size are same. If you will make your myImage size to less than imageView size, you can identify the difference.

Related

How to make a screen cut of a scroll window with the whole part not only the visible part on mac

I am writing a sreen cut APP on mac OS X, i can only get the visible part of window like this:
CGImageRef imageRef = CGWindowListCreateImage(CGRectNull,
kCGWindowListOptionIncludingWindow,
self.windowID,
kCGWindowImageBoundsIgnoreFraming);
_windowImage = [[NSImage alloc] initWithCGImage:imageRef size:self.windowFrame.size];
CGImageRelease(imageRef);
But when the window can scroll, i want to get the whole part not only the visible part, does anyone have some advices?
Thanks!
//capture 获取scrollview的截图
NSRect bounds = NSMakeRect(0, 0, [self.scrollView.documentView frame].size.width,[self.scrollView.documentView frame].size.height);
// //只能获取可见部分 only can grap the visible part of the scrollview
// NSBitmapImageRep* captureImage = [(NSView *)self.scrollView.documentView bitmapImageRepForCachingDisplayInRect:bounds];
// [self.scrollView cacheDisplayInRect:bounds toBitmapImageRep:captureImage];
// NSImage *image = [[NSImage alloc] initWithCGImage:[captureImage CGImage] size:captureImage.size];
//can grab the whole part
[self.scrollView lockFocus];
NSImage *image = [[NSImage alloc] initWithData:[(NSView *)self.scrollView.documentView
dataWithPDFInsideRect:bounds]];
[self.scrollView unlockFocus];

Change UIImage imageOrientation metadata without affecting the image

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.

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

Cocoa Touch - Adding a UIImageView programmatically?

How can I create and position a new imageview in objective-c?
Thanks!
I tried this but it doesnt seem to do anything...
-(void)drawStars{ //uses random numbers to display a star on screen
//create random position
int xCoordinate = arc4random() % 10;
int yCoordinate = arc4random() % 10;
UIImageView *starImgView = [[UIImageView alloc] initWithFrame:CGRectMake(xCoordinate, yCoordinate, 58, 40)]; //create ImageView
starImgView.image = [UIImage imageNamed:#"star.png"];
[starImgView release];
I placed this method in my viewcontroller. I see some CG stuff do I need to import core graphics or something? (what is core graphics anyway?)
You are asking about iPhone image view. Right? Then try this
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
This will create an image view of 100 x 50 dimension and locating (10, 10) of parent view.
Check the reference manual for UIImageView and UIView. You can set an image by using image property of imageView.
imgView.image = [UIImage imageNamed:#"a_image.png"];
And you can use the contentMode property of UIView to configure how to fit the image in the view. For example you can use
imgView.contentMode = UIViewContentModeCenter to place the desired image to the center of the view. The available contentModes are listed here
You haven't added your view as a subview to another view, meaning it isn't in the view hierarchy.
Assuming you are doing this in a view controller, it might look something like:
[self.view addSubview: imgView];
(void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width, self.view.layer.frame.size.height)];
imgView.image = [UIImage imageNamed:#"emptyCart.jpeg"];
imgView.backgroundColor = [UIColor whiteColor];
imgView.contentMode = UIViewContentModeCenter;
[self.view addSubview: imgView];
}
I had problem with passing the value NSSArray to NSString,
I got the answer:
//Display all images....
NSString *imgstring= [self.allimages objectAtIndex:indexPath.row];
cell.myimages.image=[UIImage imageNamed:imgstring]; //displaying Images in UIImageView..noproblem.
// Display all numbers...
cell.mylables.text=[NSString stringWithFormat:#"%#", [mysetobjectAtIndex:indexPath.row ]]; //showing results fine ArghyA..

Center & No repeat View.BackgroundColor (Iphone)

I put a background in my view like this:
UIImage *picture = [UIImage imageNamed:#"myImage.jpg"];
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:picture];
The problem is my picture is not big enough for the screen, so it repeat many times and its also not center in the View. How can I change that?
Thank you,
If you want to use a background image like this, then use a UIImageView, rather than the backgroundColor property of a standard view. In Interface Builder, add a UIImageView, size it to the same size as your view (adjust the struts and springs to match), give it your image, and make your view transparent.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
//view. =[UIImage imageNamed:#"15-tags.png"] ;//[UIColor redColor];
UIGraphicsBeginImageContext(view.frame.size);
int iImageWid=30;
int iImageHei=30;
CGRect rcCenter=CGRectMake(view.frame.origin.x+(view.frame.size.width-iImageWid)/2, view.frame.origin.y+(view.frame.size.height-iImageHei)/2, iImageWid, iImageHei);
[[UIImage imageNamed:#"15-tags.png"] drawInRect:rcCenter];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
view.backgroundColor = [UIColor colorWithPatternImage:image];