【IOS】读取、保存图片的各种方法
一.读取图片
1 UIImage* image=[UIImage imageNamed:@"1.jpg"];
1 NSURL *url=[NSURL URLWithString:@"http://www.sinaimg.cn/qc/photo_auto/chezhan/2012/50/00/15/80046_950.jpg"]; 2 UIImage *imgFromUrl =[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:url]];
1 //读取本地图片非resource 2 NSString *aPath3=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"]; 3 UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3]; 4 UIImageView* imageView3=[[UIImageView alloc]initWithImage:imgFromUrl3];
1 //add ImageIO.framework and #import 2 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL); 3 CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL); 4 CGContextRef ctx=UIGraphicsGetCurrentContext(); 5 CGContextSaveGState(ctx); 6 //transformCTM的2种方式 7 //CGContextConcatCTM(ctx, CGAffineTransformMakeScale(.2, -0.2)); 8 //CGContextScaleCTM(ctx,1,-1); 9 //注意坐标要反下,用ctx来作为图片源 10 CGImageRef capture=CGBitmapContextCreateImage(ctx); 11 CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]); 12 CGContextDrawImage(ctx, CGRectMake(160, 230, 160, 230), img); 13 CGImageRef capture2=CGBitmapContextCreateImage(ctx);
1 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL); 2 CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
1 //保存图片 2种获取路径都可以 2 //NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 3 //NSString*documentsDirectory=[paths objectAtIndex:0]; 4 //NSString*aPath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",@"test"]]; 5 NSString *aPath=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"]; 6 NSData *imgData = UIImageJPEGRepresentation(imgFromUrl,0); 7 [imgData writeToFile:aPath atomically:YES];
用UIImageWriteToSavedPhotosAlbum往照片库里面存图片时,经常发生缩略图能看到但原图消失的问题
保存图片拓展:有时候应用可能被手机禁止访问相册,导致图片保存失败
应用中有时我们会有保存图片的需求,如利用UIImagePickerController用IOS设备内置的相机拍照,或是有时我们在应用程序中利用UIKit的 UIGraphicsBeginImageContext,UIGraphicsEndImageContext,UIGraphicsGetImageFromCurrentImageContext方法创建一张图像需要进行保存。1 void UIImageWriteToSavedPhotosAlbum ( 2 UIImage *image, 3 id completionTarget, 4 SEL completionSelector, 5 void *contextInfo 6 ); 7 参数说明: 8 image 9 带保存的图片UImage对象 10 completionTarget 11 图像保存至相册后调用completionTarget指定的selector(可选) 12 completionSelector 13 completionTarget的方法对应的选择器,相当于回调方法,需满足以下格式 14 15 - (void) image: (UIImage *) image 16 didFinishSavingWithError: (NSError *) error 17 contextInfo: (void *) contextInfo;
contextInfo指定了在回调中可选择传入的数据。
当我们需要异步获得图像保存结果的消息时,我们需要指定completionTarget对象以及其completionSelector对应的选择器。示例如下:
1 - (void)saveImageToPhotos:(UIImage*)savedImage 2 { 3 UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); 4 } 5 // 指定回调方法 6 - (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo 7 { 8 NSString *msg = nil ; 9 if(error != NULL){ 10 msg = @"保存图片失败" ; 11 }else{ 12 msg = @"保存图片成功" ; 13 } 14 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示" 15 message:msg 16 delegate:self 17 cancelButtonTitle:@"确定" 18 otherButtonTitles:nil]; 19 [alert show]; 20 } 21 22 // 调用示例 23 UIImage *savedImage = [UIImage imageNamed:"savedImage.png"]; 24 25 [self saveImageToPhotos:savedImage];
三.绘制图(draw|painting)
1 UIImageView* imageView=[[UIImageView alloc]initWithImage:image]; 2 imageView.frame=CGRectMake(0, 0, 320, 480); 3 [self addSubview:imageView]; 4 [imageView release];
1 [image4 drawAtPoint:CGPointMake(100, 0)];
1 CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);
4.CGLayer
1 CGLayerRef cg=CGLayerCreateWithContext(ctx, CGSizeMake(320, 480), NULL); 2 //需要将CGLayerContext来作为缓存context,这个是必须的 3 CGContextRef layerContext=CGLayerGetContext(cg); 4 CGContextDrawImage(layerContext, CGRectMake(160, 230, 160, 230), img); 5 CGContextDrawLayerAtPoint(ctx, CGPointMake(0, 0), cg);
1 UIImage* image=[UIImage imageNamed:@"1.jpg"]; 2 CALayer *ly=[CALayer layer]; 3 ly.frame=CGRectMake(0, 0, 320, 460); 4 ly.contents=[image CGImage]; 5 [self.layer addSublayer:ly];
四.其它
1 CGImage cgImage=[uiImage CGImage]; 2 UIImage* uiImage=[UIImage imageWithCGImage:cgImage];
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。