在iphone库中检索图像

时间:2011-12-06 20:53:26

标签: iphone objective-c xcode uiimageview uiimage

我使用此代码从Facebook个人资料中获取图像并在UIImageView上显示

 // Get the profile image
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[result objectForKey:@"pic"]]]];

        // Resize, crop the image to make sure it is square and renders
        // well on Retina display
        float ratio;
        float delta;
        float px = 100; // Double the pixels of the UIImageView (to render on Retina)
        CGPoint offset;
        CGSize size = image.size;
        if (size.width > size.height) {
            ratio = px / size.width;
            delta = (ratio*size.width - ratio*size.height);
            offset = CGPointMake(delta/2, 0);
        } else {
            ratio = px / size.height;
            delta = (ratio*size.height - ratio*size.width);
            offset = CGPointMake(0, delta/2);
        }
        CGRect clipRect = CGRectMake(-offset.x, -offset.y,
                                     (ratio * size.width) + delta,
                                     (ratio * size.height) + delta);
        UIGraphicsBeginImageContext(CGSizeMake(px, px));
        UIRectClip(clipRect);
        [image drawInRect:clipRect];
        UIImage *imgThumb =   UIGraphicsGetImageFromCurrentImageContext();
        [imgThumb retain];
        imageFb=imgThumb;
        [profilePhotoImageView setImage:imgThumb];

我想制作一个按钮,点击时允许用户通过iPhone库中的图像更改图像(用iPhone拍摄的照片......)。我不知道该怎么办,请帮忙

1 个答案:

答案 0 :(得分:2)

使用UIImagePickerController最容易调解对用户照片库的访问。您可以创建,配置和实例,设置委托并以模态方式显示它。实施-imagePickerController:didFinishPickingMediaWithInfo以在用户选择图像时收到通知。然后,您可以将此图像保留在用户的文档目录中。

UIImagePickerController Class Reference