ios - 从 UITextView 获取图像

标签 ios objective-c ios7

关于如何从 UITextView 获取图像有什么想法吗?

NSString *string=@"";
self.textview.text=string;
UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 320)];
image.image=_textview.textContainer;

我也是这么想的,但是不对

最佳答案

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
}
}

关于ios - 从 UITextView 获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25367091/

相关文章:

ios - 根据计数重复一张图片

iphone - segue.destinationViewController 究竟做了什么?

ios7 - iOS 7-UITextView大小的字体以使所有文本适合 View (无滚动)

ios - Swift - 如何从 gpx 文件中读取坐标

ios - AVFoundation 捕捉静止图像太慢

ios - UITableView 在 UITableViewCell 内容 View 中显示 UIDatePicker

ios - 如何从 ALAsset 对象获取全景图

ios - 创建默认为 NSInteger 的枚举值

ios - 如何裁剪带有重复半圆的 UIView?

iphone - 向后或向前滑动时检查 UIPageViewController