UIScrollview不能平滑滚动,没有分页,也没有滑动计算

时间:2014-09-08 06:47:12

标签: ios objective-c iphone uiscrollview

我正在以编程方式将UIButtons背景图片添加到UIScrollview。如果我添加了4到5 UIButtons,那么我收到内存警告,我的应用程序退出。我不明白优化我的下面的代码有什么问题,有人可以让我知道吗?我正在使用ARC。仅在iOS4和iOS 7.1.2上出现此问题。在iPad上,最新的iOS工作正常。

- (void)showAttachment
{
   // UIImage* imgVideoThumbnail=[self getThumbNailFromURL:[NSURL URLWithString:self.notesEntity.videoPath]];

    NSMutableDictionary* dict=[NSMutableDictionary dictionary];

    NSArray* arr=[self.notesEntity.imagePath componentsSeparatedByString:@"->"];
    if (imgVideoThumbnail)
        [dict setObject:imgVideoThumbnail forKey:self.notesEntity.videoPath];
    for (NSString* stringPath in arr)
    {
        if ([stringPath length]>2)
        {

            UIImage* img=[UIImage imageWithContentsOfFile:stringPath];
            [dict setObject:img forKey:stringPath];
            img=nil;
        }
    }

    for (UIButton* view in self.scrollView.subviews)
    {
        [view removeFromSuperview];
    }

    CGRect rect=self.scrollView.bounds;
    CGFloat x=5;
    for (NSString* urlImg in dict)
    {
        UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake(x, 2, 60, 60);
        [btn setImage:[dict objectForKey:urlImg] forState:UIControlStateNormal];
        [btn setTitle:urlImg forState:UIControlStateNormal];
        btn.titleLabel.textColor=[UIColor clearColor];
        btn.layer.borderWidth=1.0;
        btn.layer.borderColor=[UIColor blueColor].CGColor;
        btn.layer.cornerRadius=7.0;
        btn.layer.masksToBounds=YES;

        [self.scrollView addSubview:btn];
        btn=nil;
        x+=105;
        if (x>self.view.bounds.size.width)
            self.scrollView.contentSize=CGSizeMake(x, rect.size.height);
    }
    dict=nil;
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSLog(@"Picker returned successfully.");
   // NSLog(@"%@", info);
    NSString     *mediaType = info[UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(__bridge NSString *)kUTTypeMovie])
    {
        NSURL *urlOfVideo = info[UIImagePickerControllerMediaURL];
        NSData* data=[NSData dataWithContentsOfURL:urlOfVideo];
        NSString* diskFilePath;
        if ([self.notesEntity.videoPath length]>2)
            diskFilePath=self.notesEntity.videoPath;
        else
            diskFilePath=[NSString stringWithFormat:@"%@/%@.MOV",self.appDeleage.notesDirectoryPath,[self.appDeleage getFileName]];
        NSError* err;

        [data writeToFile:diskFilePath options:NSDataWritingAtomic error:&err];
        if (err==nil)
        {
            self.notesEntity.videoPath=diskFilePath;
            //[self showAttachment];
        }
        data=nil;
    }
    else if ([mediaType isEqualToString:(__bridge NSString *)kUTTypeImage])
    {
        UIImage *theImage = info[UIImagePickerControllerOriginalImage];
        NSData* data=UIImagePNGRepresentation(theImage);
        NSError* err;
        NSString* diskFilePath=[NSString stringWithFormat:@"%@/%@.png",self.appDeleage.notesDirectoryPath,[self.appDeleage getFileName]];


        [data writeToFile:diskFilePath options:NSDataWritingAtomic error:&err];
        if (err==nil)
        {
            if ([self.notesEntity.imagePath length]>2)
            {
                self.notesEntity.imagePath=[NSString stringWithFormat:@"%@->%@",self.notesEntity.imagePath,diskFilePath];
            }
            else
            {
                self.notesEntity.imagePath=diskFilePath;
            }
            NSError *error;
            if (![managedObjectContext save:&error])
            {
                NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
            }
        }
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
    info=nil;
}

0 个答案:

没有答案