Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(IBAction) screenShot: (id) sender{
- UIGraphicsBeginImageContext(sshot.frame.size);
- [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIImageWriteToSavedPhotosAlbum(viewImage,nil, nil, nil);
- }
- UIGraphicsBeginImageContext(CGSize 150,150);
- //first we will make an UIImage from your view
- UIGraphicsBeginImageContext(self.view.bounds.size);
- [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- //now we will position the image, X/Y away from top left corner to get the portion we want
- UIGraphicsBeginImageContext(sshot.frame.size);
- [sourceImage drawAtPoint:CGPointMake(-50, -100)];
- UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);
- -(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect
- {
- CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
- UIImage *img = [UIImage imageWithCGImage:imageRef];
- CGImageRelease(imageRef);
- return img;
- }
- UIImage *img = [self cropImage:viewImage rect:CGRectMake(150,150,100,100)]; //example
- UIGraphicsBeginImageContext(sshot.frame.size);
- CGContextRef c = UIGraphicsGetCurrentContext();
- CGContextTranslateCTM(c, 150, 150); // <-- shift everything up to required position when drawing.
- [self.view.layer renderInContext:c];
- UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
- - (UIImage *) croppedPhoto
- {
- CGFloat ox = self.scrollView.contentOffset.x;
- CGFloat oy = self.scrollView.contentOffset.y;
- CGFloat zoomScale = self.scrollView.zoomScale;
- CGFloat cx = (ox + self.cropRectangleButton.frame.origin.x + 15.0f) * 2.0f / zoomScale;
- CGFloat cy = (oy + self.cropRectangleButton.frame.origin.y + 15.0f) * 2.0f / zoomScale;
- CGFloat cw = 300.0f / zoomScale;
- CGFloat ch = 300.0f / zoomScale;
- CGRect cropRect = CGRectMake(cx, cy, cw, ch);
- NSLog(@"---------- cropRect: %@", NSStringFromCGRect(cropRect));
- NSLog(@"--- self.photo.size: %@", NSStringFromCGSize(self.photo.size));
- CGImageRef imageRef = CGImageCreateWithImageInRect([self.photo CGImage], cropRect);
- UIImage *result = [UIImage imageWithCGImage:imageRef];
- CGImageRelease(imageRef);
- NSLog(@"------- result.size: %@", NSStringFromCGSize(result.size));
- return result;
- }
Add Comment
Please, Sign In to add comment