张运涛

c++

   :: 首页 :: 联系 :: 聚合  :: 管理

常用链接

留言簿(4)

搜索

  •  

最新评论


1.将一个指定的图形放大或缩小为指定的尺寸可以试试以下代码

-(UIImage*)scaleToSize:(UIImage*)img size:(CGSize)size  

{  

    // 创建一个bitmapcontext  

    // 并把它设置成为当前正在使用的context  

    UIGraphicsBeginImageContext(size);  

    

    // 绘制改变大小的图片  

    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];  

    

    // 从当前context中创建一个改变大小后的图片  

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  

    

    // 使当前的context出堆栈  

    UIGraphicsEndImageContext();  

    

    // 返回新的改变大小后的图片  

    return scaledImage;  

}


2.图片如何存入 iPhone 本地 Documents 的方法

UIButtonsaveImage 方法里添加一张图片以下代码可将图片存入本地的 Documents:


-(void)saveImage

{

    NSANSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];

    //also be .jpg or another

    

    UIImage *image = imageView.image; // imageView is your image

    // Returns the data for the specified image in JPEG/PNG format.

    NSData *imageData = UIImagePNGRepresentation(image);    //UIImageJPEGRepresentation(image)

    [imageData writeToFile:savedImagePath atomically:NO];  

}


3. UIImageWriteToSavedPhotosAlbum保存图片的方法

UIImageWriteToSavedPhotosAlbum往照片库里面存图片时经常发生缩略图能看到但原图消失的问题


UIImageWriteToSavedPhotosAlbum(imageSave, nil, nil, nil), imageSaveUIImage类型这样就保存进去了


而且注意图片不宜过大以免程序崩溃


4.保存已选取的图片下次启动App时直接显示的方


如果您想在iPhone App里实现保存一张图片下次再启动该App时直接自动显示该图的功能可以使用以下方法


NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

[NSData writeToFile: imagePath atomically:YES];

保存起来启动时再读出就OK

UIImage *_image = [[UIImage alloc]initWithContentsOfFile: imagePath];


5.在以图片为背景的view上直接写文字

-(void)drawAtPoint:(CGPoint)point withFont:(UIFont*)font orientation:(WBOrientation)orient;

-(CGPoint)drawInRect:(CGRect)rect withFont:(UIFont*)font orientation:(WBOrientation)orient alignment:(UITextAlignment)align;

UIFont * font = [UIFont fontWithName:@"Arial" size:18];  

NSString *str = @"Hello World";

[str  drawAtPoint:CGPointMake(0,70) withFont:font];


6.iPhone/iPad 图片库中读取图片的代码

如果您的App涉及到从iPhone/iPad图片库读取图片不妨看看CocoaChina版主“angellixf”分享的代码会为您节省很多时间

UIImagePickerController * picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentModalViewController:picker animated:YES];


UIImagePickerControllerSourceTypePhotoLibrary,// 相片库

UIImagePickerControllerSourceTypeCamera//相机获取图片

UIImagePickerControllerSourceTypeSavedPhotosAlbum// 这个是自定义库是由用户截图或保存到里面的


将图片保存到相片库的代码

UIImageWriteToSavedPhotosAlbum(Image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);


posted on 2010-09-03 17:23 张运涛 阅读(732) 评论(0)  编辑 收藏 引用 所属分类: iphone

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理