有关如何从UITextView获取图像的任何想法?
NSString *string=@"";
self.textview.text=string;
UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 320)];
image.image=_textview.textContainer;
我在想这样的事情,但不正确
答案 0 :(得分:1)
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;
}
/ 将图像保存到相册的代码 /
/* 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
}
}
这是我用来保存整个屏幕快照的代码
- (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
}
}