My image will not display and I've tried other images as well, I just get a blank screen.
UIImage *img = [[UIImage alloc] initWithContentsOfFile:#"building-demolition4.gif"];
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake(0, 0, 208.5, 119));
UIImageView *ckImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 208.5, 119)];
ckImage.image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
[self.view addSubview:ckImage];
The problem is that initWithContentsOfFile: requires a file path. #"building-demolition4.gif" is not a file path - it's just a bare name. So the runtime does not know where to look for the file - and is thus failing to find it.
If this image is inside your app bundle (because it was part of your project), the simple way to access it is with the UIImage class method imageNamed:.
(The more complex way is to get the path to the image with the NSBundle method pathForResource:..., but there is really no need for that here.)
I have used following code which works for me:
NSString *fileName = [[NSBundle mainBundle] pathForResource:#"myimg" ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFile:fileName];
CGImageRef cgimg = image.CGImage;
UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[imgview setImage:[UIImage imageWithCGImage:cgimg]];
[self.view addSubview:imgview];
if you want to add gif file have look to following link might help you.
https://github.com/unixpickle/Giraffe
Related
I am trying to figure out to get a single image pit of 2 image views like instagram
These are the 2 images. Thanks in advance.
UIImageView *photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0f, 42.0f, 280.0f, 280.0f)];
[photoImageView setBackgroundColor:[UIColor blackColor]];
[photoImageView setImage:self.image];
[photoImageView setContentMode:UIViewContentModeScaleAspectFit];
//Add overlay
UIImage *overlayGraphic = [UIImage imageNamed:#"chiu"];
UIImageView *overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
overlayGraphicView.frame = CGRectMake(30, 100, 260, 200);
[photoImageView addSubview:overlayGraphicView];
I'm not sure you can do this directly with just a UIImageView control. I think you're going to have to get into low-level drawing routines to get this done.
Option 1 (not recommended, just trying to answer the original question):
Have you tried placing the overlay UIImageView on top of the "main" UIImageView and setting its opacity to something less than 1 (say 0.4)? It's a crude hack, but it might get you somewhere.
Option 2 (probably the better path to travel):
Create an image context and then draw your "base" and "overlay" images on it. Then you'll have a UIImage you can output and will only need 1 UIImageView. Something like this (NOTE: this is a basic outline; you will need to add a LOT of time to get exactly what you want out of it!):
UIImage *baseImage = [UIImage imageNamed:#"base"];
UIImage *overlayImage = [UIImage imageNamed:#"overlay"];
UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);
CGContextDrawImage(context, rect, baseImage.CGImage);
CGContextDrawImage(context, rect, overlayImage.CGImage);
UIImage *combined = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I want to merge multiple UIImages in iOS.
To do this, I tried to do as below:
UIImage *imageLeft = [UIImage imageNamed:#"ico-left"];
UIImage *imageRight = [UIImage imageNamed:#"ico-right"];
CGSize size = CGSizeMake(imageLeft.size.width + imageRight.size.width, imageLeft.size.height);
UIGraphicsBeginImageContext(size);
[imageLeft drawInRect:CGRectMake(0, 0, imageLeft.size.width, imageLeft.size.height)];
[imageRight drawInRect:CGRectMake(imageLeft.size.width, 0, imageRight.size.width, imageRight.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];
imageView.image = finalImage;
[cell.contentView addSubview:imageView];
But I cannot get any image. How can I fix it?
Thanks.
I think you are not adding extension of image (.png) so change your upper two lines by these
UIImage *imageLeft = [UIImage imageNamed:#"ico-left.png"];
UIImage *imageRight = [UIImage imageNamed:#"ico-right.png"];
I am not sure whether this is your problem but you can give it a try. Sometimes it worked for me
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"ico-left" ofType:#"png"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:#"ico-right" ofType:#"png"];
UIImage *imageLeft = [[UIImage alloc] initWithContentsOfFile:path1];
UIImage *imageRight = [[UIImage alloc] initWithContentsOfFile:path2];
Here's the code I use to merge images. (I think I found all or part of it online at some point). Put it in a UIImage category.
// NOTE! this method should only be called from the main thread because of
// UIGraphicsGetImageFromCurrentImageContext();
- (UIImage *)merge:(UIImage *)image atRect:(CGRect)pos overlay:(BOOL)overlay fillColor:(UIColor *)fill
{
UIGraphicsBeginImageContext(self.size);
UIImage *bottom = (overlay)?self:image;
UIImage *top = (overlay)?image:self;
CGRect lf = CGRectMake(0, 0, self.size.width, self.size.height);
CGRect bottomRect = (overlay)?lf:pos;
CGRect topRect = (overlay)?pos:lf;
if (fill){
[fill setFill];
CGContextFillRect (UIGraphicsGetCurrentContext(), topRect);
}
[bottom drawInRect:bottomRect];
[top drawInRect:topRect];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
I'm based on this code:Custom MKAnnotationView with frame,icon and image
And I'm trying to do it a little different...I have an image non-transparent, and I want put an image inside.
My problem is that the background image cover my image. So it looks something like: http://cl.ly/image/0E0S00072G1n
My code is so:
annotationImageView.image = [UIImage imageNamed:#"marker_fb"];
UIImage *frame = [UIImage imageNamed:#"marker_fb"];
ImagePin *imagePinImage = annotation;
NSString *urlStringForImage = [NSString stringWithFormat:#"%#", imagePinImage.image];
NSURL *urlForImage = [NSURL URLWithString:urlStringForImage];
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#", urlForImage]] placeholderImage:[UIImage imageNamed:#"avatar-placeholder.png"]];
UIGraphicsBeginImageContext(CGSizeMake(annotationImageView.image.size.width, annotationImageView.image.size.height));
[frame drawInRect:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[imageView.image drawInRect:CGRectMake(2, 2, 48, 38)]; // the frame your inner image
annotationImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Does z-indez exist in object-c? or how should I do??
Thanks.
EDIT: I tried also with:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(annotationImageView.image.size.width, annotationImageView.image.size.height), NO, 0);
The following code splits an image into 2. It seems working fine with non-retina devices, however it gives a different output with retina devices. Could someone please help me fix it? Thanks..
My Code
UIImage *img = [UIImage imageNamed:#"apple.png"];
CGSize sz = [img size];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width/2, sz.height), NO, 0);
[img drawAtPoint:CGPointMake(-sz.width/2, 0)];
UIImage *right = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
rightView = [[[UIImageView alloc] initWithImage:right] autorelease];
rightView.frame = CGRectMake(self.view.frame.size.width/2, 0, self.view.frame.size.width/2, self.view.frame.size.height);
CGImageRef leftRef = CGImageCreateWithImageInRect([img CGImage],CGRectMake(0,0,sz.width/2,sz.height));
UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width/2, sz.height), NO, 0);
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, CGRectMake(0,0,sz.width/2.0,sz.height), leftRef);
UIImage *left = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *rotatedImage = [left imageRotatedByDegrees:180.0];
leftView = [[[UIImageView alloc] initWithImage:rotatedImage] autorelease];
leftView.frame = CGRectMake(0, 0, self.view.frame.size.width/2, self.view.frame.size.height);
leftView.transform = CGAffineTransformMake(-1,0,0,1,0,0);
CGImageRelease(leftRef);
[self.view addSubview:leftView];
[self.view addSubview:rightView];
non-retina
retina
PS: I don't know if this is important but apple.png has a #2x version..
The [-UIImage size] property returns the size in points, not in pixels. You probably need to also call [-UIImage scale] to figure out how the image is scaled.
When you create the left view with
leftView = [[[UIImageView alloc] initWithImage:rotatedImage] autorelease];,
you're not specifying the correct scale. Rather than creating your UIImage this way:
UIImage *left = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *rotatedImage = [left imageRotatedByDegrees:180.0];
try creating a CGImageRef and then initializing the UIImage using
[UIImage imageWithCGImage:scale:orientation:]
while specifying the correct scale. There are a number of ways to convert the raw image data from the context to a CGImageRef, or you can use the image you've created and use the CGImage property of UIImage.
i have the following code that i mostly got from SO but my image still is not getting cropped, what am i doing wrong?
#synthesize TopImage;
#synthesize BottomImage;
#synthesize RotaterImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
TopImage = [UIImage imageNamed:#"zero.png"];
[TopImage drawAtPoint:CGPointMake(0, 0)];
[self cropImage:TopImage:0];
[self addSubview:[[UIImageView alloc] initWithImage:TopImage]];
// BottomImage = [UIImage imageNamed:#"zero.png"];
// [BottomImage drawAtPoint:CGPointMake(0, 55)];
// [self cropImage:BottomImage:50];
// [self addSubview:[[[UIImageView alloc] initWithImage:BottomImage]retain]];
// RotaterImage = [UIImage imageNamed:#"zero.png"];
// [RotaterImage drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:0];
// [self addSubview:[[[UIImageView alloc] initWithImage:RotaterImage]retain]];
// [self cropImage:RotaterImage:50];
}
return self;
}
-(void)cropImage:(UIImage *)image:(int)startCroppingPosition{
CGRect tempRect = CGRectMake(0, startCroppingPosition, 23, 40);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], tempRect);
// or use the UIImage wherever you like
image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
}
I am very new to IOS so any help is appreciated
You're creating a new cropped image but not telling the init method about it :)
Try this change :
-(UIImage *)cropImage:(UIImage *)image:(int)startCroppingPosition{
CGRect tempRect = CGRectMake(0, startCroppingPosition, 23, 40);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], tempRect);
// or use the UIImage wherever you like
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return newImage;
}
and, in init, replace
[TopImage drawAtPoint:CGPointMake(0, 0)];
with
UIImageView* topImageView = [[UIImageView alloc] initWithImage:topImage];
topImageView.frame = CGRectMake(x,y,width,height);
Your crop method now returns the newly cropped image and stores it in TopImage.
Other advice :
Class names begin with a CapitalLetter, variables begin with lowercase. TopImage should really be topImage.
Also, I would always use named parameters in your methods, so
-(void)cropImage:(UIImage *)image:(int)startCroppingPosition
should be something like
-(void)cropImage:(UIImage *)image startPosition:(int)startCroppingPosition
That will make your code much much more readable when you come back to it in a month's time (I've learned that the hard way in the past!)
You are only changing the image and not the image of the UIImageView,
What I would do is replace:
[self addSubview:[[UIImageView alloc] initWithImage:TopImage]];
with
UIImageView *iv = [[UIImageView alloc] initWithImage:TopImage]];
iv.tag = 1000 //number does not matter
[self.view addSubview:iv];
[iv release];
then in crop image you just add:
UIImageView *iv = [self.vief viewWithTag:1000];
iv.image = [UIImage imageWithCGImage:imageRef];
This should then work as expected!