This is my drawInContext method:
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"myImage" ofType:#"png"]];
CGRect cropRect = CGRectMake(0, 0, 175, 175);
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], cropRect);
CGRect imageRect;
imageRect.origin = CGPointMake(0, 0);
imageRect.size = CGSizeMake(175, 175);
CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, imageRef);
CGImageRelease(imageRef);
My image is upside down, how can I change it? What's wrong with my code? How can I change the image become normal?? thx .
Instead of this:
CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, imageRef);
do this:
[img drawInRect:CGRectMake(0, 0, 175, 175)]
CGContextDrawImage doesn't preserve orientation.
Related
I need to get the screenshot of a video on click of a button.Below is the code used for that,but the image shows only the bottom part of the video that is the play and progress with the time passed..I have used MPMoviePlayerController for the video to play.
CGRect rect = [_moviePlayer.view bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[_moviePlayer.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[imgView setImage:image];
imgView.layer.borderWidth = 2.0;
[_firstimage addSubview:imgView];
NSURL *url=[NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
this is how it shows..How can i get the video image? Can anybody help me..
How about using [MPMoviePlayerController requestThumbnailImagesAtTimes: timeOption:]?
CGImageRef originalImage = UIGetScreenImage();
CGImageRef videoImage = CGImageCreateWithImageInRect(originalImage, CGRectMake(0, 0, 680, 300));
UIImage *snapShotImage = [UIImage imageWithCGImage:videoImage];
UIImageWriteToSavedPhotosAlbum(snapShotImage, nil, nil, nil);
CGImageRelease(originalImage);
CGImageRelease(videoImage);
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 250, 200)];
[imgView setImage:snapShotImage];
imgView.layer.borderWidth = 2.0;
[_firstimage addSubview:imgView];
this code works...thanks for the help...
I am trying to send an image through sockets to a server, but the image doesn't appear well on the server side.
I am using this code, what can be the problem? I am sending text to the server and it works, but the image appears unformatted.
UIImage *imageResized=[self resizeImage:editedImage newSize:CGSizeMake(64,32)];
NSData *imageData = UIImageJPEGRepresentation(imageResized, 1.0f);
NSString *encodedString = [imageData base64EncodedStringWithOptions:nil];
NSString *finalStr= [NSString stringWithFormat:#"image:%#",encodedString];
NSData* data = [finalStr dataUsingEncoding:NSASCIIStringEncoding];
[tcpsocket initNetworkCommunication:[tcpSocket linkWebserviceInet1] porta:[tcpSocket linkWebport]];
[tcpsocket sendNSData:data];
- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);
CGContextConcatCTM(context, flipVertical);
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
UIGraphicsEndImageContext();
return newImage;
}
I'm rendering UIImage in the - drawRect: and placing it in the passed in CGRect by calling drawInRect: rect on the UIImage. It automatically resizes and stretches itself, which I don't want. How can I keep it the original size?
UIImage* left = [UIImage imageNamed: #"Map_info_bubble_left"];
UIImage* center = [UIImage imageNamed: #"Map_info_bubble_center"];
UIImage* right = [UIImage imageNamed: #"Map_info_bubble_right"];
UIImage* leftCapStretched = [left resizableImageWithCapInsets: UIEdgeInsetsMake(0, 20, 0, 0)];
UIImage* rightCapStretched = [right resizableImageWithCapInsets: UIEdgeInsetsMake(0, 0, 0, 15)];
CGFloat fullWidth = leftCapStretched.size.width + center.size.width + rightCapStretched.size.width;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(fullWidth, center.size.height), NO, 0);
[leftCapStretched drawAtPoint: CGPointMake(0, 0)];
[center drawAtPoint: CGPointMake(leftCapStretched.size.width, 0)];
[rightCapStretched drawAtPoint: CGPointMake(left.size.width + center.size.width, 0)];
UIImage* callout = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[callout drawInRect: rect];
Essentially what is happening is the center image should remain the original size while the caps stretch, but the center is stretching and the caps don't stretch enough. I noticed that if I multiply fullWidth by 2, it shrinks the center closer to what it should be.
EDIT: More simply asked after reading the documentation a bit more, is there a way to prevent the UIImage from scaling to fit the rect when drawInRect: is called?
This can be done simply by passing the images size property to drawInRect:
e.x:
[myImage drawInRect:CGRectMake(someOriginX, someOriginY, myImage.size.width, myImage.size.height)];
so I have this function:
- (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage {
UIImage *image = nil;
UIImageView *temp_img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
temp_img.image = secondImage;
secondImage = temp_img.image;
CGSize newImageSize = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), MAX(firstImage.size.height, secondImage.size.height));
if (UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]);
} else {
UIGraphicsBeginImageContext(newImageSize);
}
[firstImage drawAtPoint:CGPointMake(roundf((newImageSize.width-firstImage.size.width)/2),
roundf((newImageSize.height-firstImage.size.height)/2))];
[secondImage drawAtPoint:CGPointMake(roundf((100)),
roundf((100)))];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Im trying to change the size of the secondImage UIImage so that when they merge I can adjust the size to make it look good.
Any help is appreciated thanks guys!
Note the "0" in UIGraphicsBeginImageContextWithOptions:
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);
CGContextConcatCTM(context, flipVertical);
...
// make anySize the size you want and pt any point
CGContextDrawImage(context, (CGRect){ pt1, anySize1 }, [first CGImage]);
// make anySize the size you want and pt any point
CGContextDrawImage(context, (CGRect){ pt2, anySize2 }, [secondImage CGImage]);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
I have a function that will overlay images over another image and return as a UIImage. However, a white line-ish thing comes up on it. Here's what I have
- (UIImage * ) romanticFilterImage: (UIImage *) imageA {
UIImage *vintageOne = [UIImage imageNamed:#"filter_vintage_1.png"];
UIImage *vintageTwo= [UIImage imageNamed:#"filter_vintage_2.png"];
UIImage *vintageThree = [UIImage imageNamed:#"filter_vintage_3.png"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageA.size.width, imageA.size.height), YES, 0.0);
[imageA drawAtPoint: CGPointMake(0,0)];
[vintageThree drawInRect:CGRectMake(0, 0, imageA.size.width, imageA.size.height) blendMode: kCGBlendModeMultiply alpha: 1.0];
[vintageTwo drawInRect:CGRectMake(0, 0, imageA.size.width + 2, imageA.size.height + 2) blendMode: kCGBlendModeColorBurn alpha: 1.0];
[vintageOne drawInRect:CGRectMake(0, 0, imageA.size.width, imageA.size.height) blendMode: kCGBlendModeMultiply alpha: 1.0];
UIImage *source = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize size = [source size];
UIGraphicsBeginImageContext(size);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 5.0, 5.0, 5.0, 5.0);
CGContextStrokeRect(context, rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
Here is a screenshot: (I have added a black background to enhance the slighly transparent border.
Aren't you stroking the image white in these lines?
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 5.0, 5.0, 5.0, 5.0);
CGContextStrokeRect(context, rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Although I am not sure if you intended GContextSetRGBStrokeColor(context, 5.0, 5.0, 5.0, 5.0); as 1.0 is the maximum value for the rgba values in the function.