uiimage的一些用法

1.保存到iOS照片库需要引入QuartzCore.framework框架,具体代码如下:
.h文件
#import <QuartzCore/QuartzCore.h>
UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
.m文件
UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *temp = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(temp, nil, nil, nil);

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

//        //以下是保存文件到沙盒路径下

//        //把图片转成NSData类型的数据来保存文件

//        NSData *data;

//        //判断图片是不是png格式的文件

//        if (UIImagePNGRepresentation(image)) {

//            //返回为png图像。

//            data = UIImagePNGRepresentation(image);

//        }else {

//            //返回为JPEG图像。

//            data = UIImageJPEGRepresentation(image, 1.0);

//        }

//        //保存

//        [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];


//设置圆形图片

- (UIImage *)circleImage

{

    // NO代表透明

    UIGraphicsBeginImageContextWithOptions(self.size,NO, 0.0);

    

    // 获得上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    // 添加一个圆

    CGRect rect = CGRectMake(0,0, self.size.width,self.size.height);

    CGContextAddEllipseInRect(context, rect);

    

    // 裁剪

    CGContextClip(context);

    

    // 将图片画上去

    [selfdrawInRect:rect];

    

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

    

    return image;

}


使用方法就是
uiimage * image =[ [uiimage alloc] initWithName:@""];
[image circleImage];  就可以得到一个圆形的图片  

猜你喜欢

转载自blog.csdn.net/cupidsCat/article/details/52334160