Reputation: 95
Any ideas on how to get an Image from UITextView?
NSString *string=@"";
self.textview.text=string;
UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 320)];
image.image=_textview.textContainer;
I was thinking something like that but its not right
Upvotes: 1
Views: 625
Reputation: 3444
NSString *string=@"write your text";
self.textview.text=string;
UIImage *image = [self makeImageWithRect:self.textview.frame];// get image from rectangle for textview
UIImageView *imageView=[[UIImageView alloc]init];
imageView.image=image;
-(UIImage*)makeImageWithRect:(CGRect)rect {
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
/code for saving image to photo album/
/* Save to the photo album */
UIImageWriteToSavedPhotosAlbum(imageToSave ,
self, // send the message to 'self' when calling the callback
@selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), // the selector to tell the method to call on completion
NULL); // you generally won't need a contextInfo here);
[[[UIAlertView alloc] initWithTitle:@"App Name" message:@"Image saved to photo album" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]show];
- (void)thisImage:(UIImage *)image hasBeenSavedInPhotoAlbumWithError:(NSError *)error usingContextInfo:(void*)ctxInfo {
if (error) {
// Do anything needed to handle the error or display it to the user
} else {
// .... do anything you want here to handle
// .... when the image has been saved in the photo album
}
}
here is the code which i use for saving a snapshot of whole screen
- (IBAction)saveAction:(id)sender {
UIView* captureView = self.ContainerView;
/* Capture the screen shoot at native resolution */
UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, captureView.opaque, 0.0);
[captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
/* Render the screen shot at custom resolution */
CGRect cropRect = self.view.frame;
UIGraphicsBeginImageContextWithOptions(cropRect.size, captureView.opaque, 1.0f);
[screenshot drawInRect:cropRect];
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *imageToSave =[UIImage imageWithCGImage:[customScreenShot CGImage]
scale:1.0
orientation: UIImageOrientationUp];
/* Save to the photo album */
UIImageWriteToSavedPhotosAlbum(imageToSave ,
self, // send the message to 'self' when calling the callback
@selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), // the selector to tell the method to call on completion
NULL); // you generally won't need a contextInfo here);
[[[UIAlertView alloc] initWithTitle:@"App Name" message:@"Image saved to photo album" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]show];
}
- (void)thisImage:(UIImage *)image hasBeenSavedInPhotoAlbumWithError:(NSError *)error usingContextInfo:(void*)ctxInfo {
if (error) {
// Do anything needed to handle the error or display it to the user
} else {
// .... do anything you want here to handle
// .... when the image has been saved in the photo album
}
}
Upvotes: 1