在viewDidLoad外部调用方法时,Camera Roll中的最后一个图像不显示

时间:2014-07-28 10:42:02

标签: ios objective-c xcode ios7 xcode5

我正在使用代码段从Camera Roll获取最后一张图片。我已经制作了一个方法,我在viewDidLoad方法中调用它。当我点击按钮时,我正在使用此功能通过Social Framework共享最后一张图像。这样一切正常。

问题是当我尝试从viewDidLoad替换我的方法并将其放在IBAction内时,图片不会在社交弹出窗口中呈现。调试显示我的方法返回它应该返回的内容,一个图像,只需按一下按钮即可触发它。由于加载此viewController时最新图像会发生变化,因此无法从viewDidLoad调用它。这是代码。

方法:

- (void) LastPic
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
        if (alAsset) {
            ALAssetRepresentation *representation = [alAsset defaultRepresentation];
            UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
            *stop = YES; *innerStop = YES;
            self.imageToShare = latestPhoto;
        }
    }];
} failureBlock: ^(NSError *error) {
    NSLog(@"No groups");
}];}

动作:

- (IBAction)ShareT:(id)sender {
[self LastPic]; //when in viewDidLoad everything kk
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
    self.slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
    [self.slComposeViewController addImage:self.imageToShare];
    [self presentViewController:self.slComposeViewController animated:YES completion:NULL];
}
else
{}}

我启用了所有必要的框架,我的h文件看起来像这样。当我尝试修改代码以将图像作为NSData传递时,会发生同样的情况。

@property (strong, nonatomic) UIImage *imageToShare;

谢谢!

1 个答案:

答案 0 :(得分:0)

这是因为ALAssets是在块中获取的。这意味着,当尚未提取图像时,您将调用共享控制器。我建议你添加一些像this一样的进度,延迟2-3秒。这将解决您的问题,并且对用户友好。要检查它是否真的有效,请测试下面的代码:

- (void)fetchLastPhoto
{
    __block UIImage *latestPhoto;

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
        if (alAsset)
        {
            ALAssetRepresentation *representation = [alAsset defaultRepresentation];
            latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
            _imageToShare = latestPhoto;
            *stop = YES; *innerStop = YES;
        }

        NSLog(@"Image fetched");
    }];
} failureBlock: ^(NSError *error) {
    NSLog(@"No groups");
}];
}

- (IBAction)buttonShare:(id)sender
{
    [self fetchLastPhoto];
    [self performSelector:@selector(openShareController) withObject:nil afterDelay:3.0f];
}

-(void)openShareController
{
    NSLog(@"Share controller called");
    SLComposeViewController *slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
    [slComposeViewController addImage:self.imageToShare];
    [self presentViewController:slComposeViewController animated:YES completion:NULL];
}