将UIImage保存到iOS照片库和对应程序沙盒中

1.保存到iOS照片库需要引入QuartzCore.framework框架,具体代码如下:
.h文件
#import<QuartzCore/QuartzCore.h>
UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIImageView*imageView;
.m文件
UIGraphicsBeginImageCont
ext(imageView.bounds.size);
[imageView.layerrenderInContext:UIGraphicsGetCurrentCont ext()];
UIImage *temp = UIGraphicsGetImageFromCu rrentImageContext();
UIGraphicsEndImageContex t();
UIImageWriteToSavedPhoto sAlbum(temp, nil, nil, nil);

2.保存到对应的沙盒目录中,具体代码如下:
NSArray *paths =NSSearchPathForDirectori esInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathCom ponent:[NSStringstringWithFormat:@"sms.gif"]];    // 保存文件的名称
BOOL result =[UIImagePNGRepresentation (imageView.image)writeToFile:filePath      atomically:YES]; // 保存成功会返回YES 

3,图片上写文字
+ (UIImage *)imageAddText:(UIImage *)img textArray:(NSArray *)textArray
{
    int w = img.size.width;
    int h = img.size.height;
    UIGraphicsBeginImageContext(img.size);
    [[UIColor blackColor] set];
    [img drawInRect:CGRectMake(0, 0, w, h)];
    
    NSInteger count = [textArray count];
    for (int i= 0; i<count; i++) {
        NSString *mark = [textArray objectAtIndex:i];
        [mark drawInRect:CGRectMake(10, 30 + i*40, w - 10, 40) withFont:[UIFont systemFontOfSize:30]];
    }
    
    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return aimg;
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值